gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/gpu/imagegen_test.go (about)

     1  // Copyright 2024 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package imagegen_test runs Stable Diffusion and generates images with it.
    16  package imagegen_test
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  	"time"
    22  
    23  	"gvisor.dev/gvisor/test/gpu/stablediffusion"
    24  )
    25  
    26  // TestStableDiffusionXL generates an image with Stable Diffusion XL.
    27  func TestStableDiffusionXL(t *testing.T) {
    28  	ctx := context.Background()
    29  	sdxl := stablediffusion.NewDockerXL(t)
    30  	generateCtx, generateCancel := context.WithTimeout(ctx, 15*time.Minute)
    31  	defer generateCancel()
    32  	image, err := sdxl.Generate(generateCtx, &stablediffusion.XLPrompt{
    33  		Query:           `A boring flat corporate logo that says "gVisor"`,
    34  		AllowCPUOffload: false,
    35  		UseRefiner:      false,
    36  		NoiseFraction:   0.8,
    37  		// This is just a test to make sure Stable Diffusion works at all,
    38  		// so we don't need a lot of steps here:
    39  		Steps: 8,
    40  	})
    41  	if err != nil {
    42  		t.Fatalf("Cannot generate image with Stable Diffusion XL: %v", err)
    43  	}
    44  	img, err := image.Image()
    45  	if err != nil {
    46  		t.Fatalf("Cannot decode image: %v", err)
    47  	}
    48  	size := img.Bounds().Size()
    49  	if size.X <= 0 || size.Y <= 0 {
    50  		t.Fatalf("Generated image has invalid size: %dx%d", size.X, size.Y)
    51  	}
    52  	ascii, err := image.ASCII()
    53  	if err != nil {
    54  		t.Fatalf("Cannot convert image to ASCII: %v", err)
    55  	}
    56  	t.Logf("Generated image (size %dx%d pixels):\n%s\n", size.X, size.Y, ascii)
    57  }