Compilation Using BiSheng Command Lines

The BiSheng Compiler, designed for AI processors, can support heterogeneous programming extension and compile Ascend operator code into binary executable files and dynamic libraries. The executable program of the BiSheng Compiler is named bisheng, which supports host systems such as x86 and AArch64. It also natively supports the compilation of AI Core architecture instruction sets on the device. By using the BiSheng Compiler, you can program and develop Ascend AI Processors more efficiently.

Example for Quick Start

The following is an example of using the BiSheng Compiler to compile and implement the Add operator using SIMT programming. This example shows how to write the source file add.asc and the specific compilation command. With this example, you can understand how to use the BiSheng Compiler to compile SIMT operators. For details about the complete sample, see LINK.

  1. Include header files.
    When writing the operator source file, you need to include necessary header files.
    1
    2
    3
    // Header files
    #include "acl/acl.h" // Header file for calling kernel function APIs
    #include "asc_simt.h" // Header file for calling SIMT APIs in the kernel function
    
  2. Implement the kernel function.
    Currently, the input parameters of the kernel function support only basic data types and their pointer types. For details about the syntax and restrictions of the kernel function, see Kernel Function.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    __global__ void add_custom(float* x, float* y, float* z, uint64_t total_length)
    {
        // Calculate global thread ID
        int32_t idx = blockIdx.x * blockDim.x + threadIdx.x;
        // Maps to the row index of output tensor
        if (idx >= total_length) {
            return;
        }
        z[idx] = x[idx] + y[idx];
    }
    
  3. Call the function logic on the host, including memory allocation and release, initialization and deinitialization, and kernel function call using the kernel launch symbol.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    // Header file to be included in the application on the host
    #include "acl/acl.h"
    
    // Implement the kernel function.
    __global__ void add_custom(float* x, float* y, float* z, uint64_t total_length)
    {
        ...
    }
    
    // Call the operator using the kernel launch symbol <<<...>>>.
    std::vector<float> add(std::vector<float>& x, std::vector<float>& y)
    {
        ...
        // Call kernel funtion with <<<...>>>
        add_custom<<<...>>>(...));
        ...
    }
    
    
    // Compare the computation result.
    uint32_t verify_result(std::vector<float>& output, std::vector<float>& golden)
    {
        ...
    }
    
    // Main program for operator verification
    int32_t main(int32_t argc, char *argv[])
    {
        constexpr uint32_t in_shape = 48 * 256;
        std::vector<float> x(in_shape);
        for (uint32_t i = 0; i < in_shape; i++) {
            x[i] = i * 1.1f;
        }
        std::vector<float> y(in_shape);
        for (uint32_t i = 0; i < in_shape; i++) {
            y[i] = i + 3.4f;
        }
        std::vector<float> golden(in_shape);
        for (uint32_t i = 0; i < in_shape; i++) {
            golden[i] = x[i] + y[i];
        }
        std::vector<float> output = add(x, y);
        return verify_result(output, golden);
    }
    
  4. Run the following command to perform compilation:
    1
    bisheng -x asc add.asc -o demo --npu-arch=dav-3510 --enable-simt
    
    • -x asc: -x specifies the compilation language, and asc indicates that the programming language is Ascend C.
    • -o demo: specifies the output file name as demo.
    • --npu-arch=dav-3510: specifies the NPU architecture version as dav-3510. The number following dav- indicates the NPU architecture version. For details about the architecture version number of each product model, see Table 1.
    • --enable-simt: fixed option for SIMT compilation.
  5. Run the executable file.
    1
    ./demo
    

Program Compilation and Execution

The BiSheng Compiler can compile operator source files (with the .asc extension) into executable files on the current platform. In addition, the -x asc compilation option can be used to compile C++/C source files with the .cpp/.c extension.
1
2
3
4
5
 # bisheng -x asc [Operator source file] -o [Output product name] --npu-arch=[NPU architecture version] --enable-simt. The common parameter sequence is the same as that of g++.
# C++ source file
bisheng -x asc add_custom.cpp -o add_custom --npu-arch=dav-xxxx --enable-simt
# Operator source file with the .asc extension
bisheng -x asc add_custom.asc -o add_custom --npu-arch=dav-xxxx --enable-simt

The generated executable file can be executed using the following command:

./add_custom

In the command line compilation scenario, you can link the required library files as needed. By default, the library files listed in Table 1 are linked during compilation.