All Products
Search
Document Center

Function Compute:Best practices for AI

Last Updated:Feb 02, 2024

You can perform the best practices for GPU-accelerated instances by using the Function Compute console, SDKs, or Serverless Devs. This topic describes how to use the Function Compute console to process raw images by using function code to perform style synthesis and object detection. In this example, Python is used.

Scenarios and benefits

In most cases, various issues occur in the traditional GPU infrastructure for AI applications. The issues include long construction cycle, high O&M complexity, low cluster utilization, and high cost. GPU-accelerated instances of Function Compute transfer the issues from the user side to the cloud vendor side. This way, you can focus on your business and achieve your business goals without the need to worry about the underlying GPU infrastructure.

This section describes the benefits of GPU-accelerated instances compared with instances that are accelerated by CPUs in Function Compute:

  • Cost-first AI application scenarios

    • Retain GPU-accelerated instances based on their business requirements. This way, GPU-accelerated instances of Function Compute offer much higher cost efficiency than self-built GPU clusters.

    • Use GPU resources in 1/2 or exclusive mode through the GPU virtualization technology. This way, GPU-accelerated instances can be configured in a fine-grained manner.

  • Efficiency-first AI application scenarios

    • Focus on code development and the business objectives without the need to perform the O&M of GPU clusters, such as driver and CUDA version management, machine operation management, and GPU bad card management.

For more information about GPU-accelerated instances, see Instance types and instance modes.

Tutorial for neural style transfer

Neural style transfer (NST) is a technology that is used to blend two images. During the process, the content is extracted from one image and the style is extracted from the other image to create a new image. In the following example, the TensorFlow Hub preset model is used to complete the stylistic synthesis of an image.

Synthesis effect

Content image

Style image

Synthesized image

tensorflow_example_dog

tensorflow_example_style_dog

tensorflow_example_result_dog

Prerequisites

  • General operations

    • To ensure that your business can run as expected, join the DingTalk group 11721331 to request the permissions to use GPU-accelerated instances and provide the following information:

      • The organization name, such as the name of your company.

      • The ID of your Alibaba Cloud account.

      • The region where you want to use GPU-accelerated instances, such as China (Shenzhen).

      • The contact information, such as your mobile number, email address, or DingTalk account.

      • The size of your image.

    • Uploaded the audio and video resources to the OSS bucket in the region where the GPU-accelerated instances are located. You have the read and write permissions on the objects in the bucket. For more information, see Upload objects. For more information, see Bucket ACL.

Use the Function Compute console to deploy a GPU application

  1. Deploy an image.

    1. Create a Container Registry Enterprise Edition instance or Container Registry Personal Edition instance.

      We recommend that you create an Enterprise Edition instance. For more information, see Create a Container Registry Enterprise Edition instance.

    2. Create a namespace and an image repository.

      For more information, see the Step 2: Create a namespace and Step 3: Create an image repository sections of the "Use Container Registry Enterprise Edition instances to build images" topic.

    3. Perform operation on Docket as prompted in the . Push the app.py and Dockerfile files in the preceding example to the image repository of the instance.

      db-acr-docker

  2. Create a GPU function. For more information, see Create a custom container function.

  3. Change the execution timeout period of the function.

    1. Click the name of the function that you want to manage. In the navigation bar of the details page, click the Configurations tab. In the Environment Information section, click Modify.

    2. In the panel that appears, configure the Execution Timeout Period parameter and click OK.

    Note

    The CPU transcoding duration exceeds the default value of 60 seconds. Therefore, we recommend that you set the Execution Timeout Period parameter to a larger value.

  4. Configure a provisioned GPU instance. For more information about how to configure provisioned instances, see Configure provisioned instances and auto scaling rules.

    After the configuration is complete, you can check whether the provisioned GPU-accelerated instances are ready in the rule list. Specifically, check whether the value of the Current Reserved Instances parameter is the specified number of provisioned instances.

  5. Use cURL to test the function.

    1. On the function details page, click the Trigger Management (URL) tab to view the configurations of the trigger and obtain the endpoint of the trigger.

    2. Run the following command in the CLI to invoke the GPU function:

      • View online function versions

        curl -v "https://tgpu-ff-console-tgpu-ff-console-ajezot****.cn-shenzhen.fcapp.run"
        {"function": "trans_gpu"}
      • Perform neural style transfer

        curl "https://tgpu-fu-console-tgpu-se-console-zpjido****.cn-shenzhen.fcapp.run" -H 'RUN-MODE: normal'
        {"result": "transfer ok"}

Verify the result

You can view the synthesized image by accessing the following domain name in a browser:

https://cri-zbtsehbrr8******-registry.oss-cn-shenzhen.aliyuncs.com/stylized-image.png

This domain name is used as an example. The actual domain name prevails.