Skip to content

Move Ascend device initialization/cleanup logic from InfiniTensor RuntimeObj into InfiniOps #600

@bitzyz

Description

@bitzyz

Background

In InfiniTensor's src/core/runtime.cc, the RuntimeObj constructor and destructor hardcode Ascend ACL device initialization and cleanup logic:
Constructor (RuntimeObj::RuntimeObj):

#ifdef WITH_ASCEND
    if (device.type() == Device::Type::kAscend) {
        auto ret = aclInit(nullptr);
        if (ret != ACL_SUCCESS) {
            fprintf(stderr, "aclInit failed: %d\n", ret);
        }
        ret = aclrtSetDevice(deviceId);
        if (ret != ACL_SUCCESS) {
            fprintf(stderr, "aclrtSetDevice failed: %d\n", ret);
        }
        ret = aclrtCreateStream(static_cast<aclrtStream *>(&stream_));
        if (ret != ACL_SUCCESS) {
            fprintf(stderr, "aclrtCreateStream failed: %d\n", ret);
        }
    }
#endif

Destructor (RuntimeObj::~RuntimeObj):

#ifdef WITH_ASCEND
    if (device.type() == Device::Type::kAscend && stream_) {
        aclrtDestroyStream(static_cast<aclrtStream>(stream_));
        aclrtResetDevice(deviceId);
        aclFinalize();
    }
#endif

Problem

Breaks layer separation: As an upper-level framework, InfiniTensor should not directly call low-level hardware SDK (ACL) initialization/cleanup functions. This logic belongs in InfiniOps' Ascend runtime module.

Inconsistent with other devices: InfiniOps already provides Runtime specializations for each device (e.g., RuntimeDevice::Type::kAscend defines Malloc, Free, Memcpy, etc.), but lacks device initialization (aclInit, aclrtSetDevice, aclrtCreateStream) and cleanup (aclrtDestroyStream, aclrtResetDevice, aclFinalize) interfaces.

InfiniTensor directly depends on ACL headers: The #include "acl/acl.h" in runtime.cc exposes InfiniTensor's core code to Ascend SDK compilation dependencies.

Proposal

Add device initialization and cleanup capabilities to InfiniOps' Runtime interface:

Add Init / SetDevice / CreateStream / DestroyStream / ResetDevice / Finalize interfaces to RuntimeDevice::Type::kAscend (or add optional Init/Destroy lifecycle interfaces to the DeviceRuntime base class).

Have InfiniTensor's RuntimeObj constructor/destructor call through InfiniOps' unified interface instead of calling ACL functions directly, thereby removing InfiniTensor's direct dependency on acl/acl.h.

Similarly, stream_ type management can be encapsulated by InfiniOps to avoid device-specific type casts like static_cast in InfiniTensor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions