Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions src/turbomind/runtime/core/DIPIRawGeneratorImpl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright (c) 2023, DeepLink.
#include "DIPURawGeneratorImpl.h"
#include "../device/rawdeviceapis.h>

namespace dipu {
/**
* DIPURawGeneratorImpl class implementation
*/
DIPURawGeneratorImpl::DIPURawGeneratorImpl() :state_need_reset_(true) {
seed_ = 0;
}

/**
* Private clone method implementation
*
* See Note [Acquire lock when using random generators]
*/
std::shared_ptr<DIPURawGeneratorImpl> DIPURawGeneratorImpl::clone() const {
auto gen = new DIPURawGeneratorImpl();
gen->set_current_seed(this->seed_);
auto state = this->state_;
const auto& state_clone = this->clone_state();
gen->set_state(state_clone);
gen->set_state_flag(this->state_need_reset_);
return std::shared_ptr<DIPURawGeneratorImpl>(&gen);
}

/**
* Sets the seed to be used by MTGP
*
* See Note [Acquire lock when using random generators]
*/
void DIPURawGeneratorImpl::set_current_seed(uint64_t seed) {
seed_ = seed;
state_need_reset_ = true;
}

/**
* Gets the current seed of DIPUGeneratorImpl.
*/
uint64_t DIPURawGeneratorImpl::current_seed() const {
return seed_;
}

/**
* Gets a nondeterministic random number from /dev/urandom or time,
* seeds the CPUGeneratorImpl with it and then returns that number.
*
*/
uint64_t DIPURawGeneratorImpl::seed() {
//TODO:随机生成seed
uint64_t random = 42;
this->set_current_seed(random);
return random;
}

/**
* get state
*
* See Note [Acquire lock when using random generators]
*/
RandState DIPURawGeneratorImpl::get_state() const {
if (state_need_reset_) {
update_state();
}
auto state_clone = this->clone_state();
return state_clone;
}

RandState DIPURawGeneratorImpl::clone_state(const RandState& state) const {
uint8_t newnum = *(state.state_);
RandState newState{&newnum, state.size_};
return newState;
}

/**
* set state
*
* See Note [Acquire lock when using random generators]
*/
void DIPURawGeneratorImpl::set_state(const RandState& state) {
this->state_ = this->clone_state(state);
}

/**
* set state flag
* See Note [Acquire lock when using random generators]
*/
void DIPURawGeneratorImpl::set_state_flag(bool flag) {
state_need_reset_ = flag;
}

} // namespace dipu
37 changes: 37 additions & 0 deletions src/turbomind/runtime/core/DIPURawGeneratorImpl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2023, DeepLink.
#pragma once

#include <memory>
#include <cstdint>

#include "basedef.h"

namespace dipu {
struct RandState {
std::share_ptr<uint8_t*> state_ = nullptr;
size_t size_ = 0;
};

class DIPURawGeneratorImpl {
public:
// Constructors
explicit DIPURawGeneratorImpl();
~DIPURawGeneratorImpl() = default;

std::shared_ptr<DIPURawGeneratorImpl> clone() const;
void set_current_seed(uint64_t seed);
uint64_t current_seed() const;
uint64_t seed();
RandState get_state() const;
RandState clone_state(const RandState& state) const;
virtual void set_state(const RandState& state) {};

void set_state_flag(bool flag);
virtual void update_state() const {};

uint64_t seed_;
mutable RandState state_;
mutable bool state_need_reset_;
}

} // namespace dipu
88 changes: 88 additions & 0 deletions src/turbomind/runtime/device/basedef.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) 2023, DeepLink.
#pragma once

// todo: move out deice dir to diopi
namespace dipu {

#define DIPU_API __attribute__ ((visibility ("default")))

#define DIPU_WEAK __attribute__((weak))

// "default", "hidden", "protected" or "internal
#define DIPU_HIDDEN __attribute__ ((visibility ("hidden")))

typedef int32_t enum_t;

#define DIPU_STRING(x) #x
#define DIPU_CODELOC __FILE__ " (" DIPU_STRING(__LINE__) ")"


#define DIPU_LOGE(fmt, ...) \
printf( \
"[ERROR]%s,%s:%u:" #fmt "\n", \
__FUNCTION__, \
__FILE__, \
__LINE__, \
##__VA_ARGS__)

#define DIPU_LOGW(fmt, ...) \
printf( \
"[WARN]%s,%s:%u:" #fmt "\n", \
__FUNCTION__, \
__FILE__, \
__LINE__, \
##__VA_ARGS__)


namespace devapis {

enum class VendorDeviceType : enum_t {
MLU, //camb
NPU, //ascend
CUDA, //cuda
GCU, //gcu
SUPA, //Biren
DROPLET, //droplet
};

enum class EventStatus: enum_t {
PENDING,
RUNNING,
DEFERRED,
READY
};

enum class OpStatus: enum_t {
SUCCESS,
ERR_UNKNOWN,
ERR_NOMEM,
};

enum class MemCPKind: enum_t {
D2H,
H2D,
D2D,
};

typedef enum {
/*! The operation was successful. */
DICL_SUCCESS = 0x0,

/*! undefined error */
DICL_ERR_UNDEF = 0x01000,

} diclResult_t;

struct DIPUDeviceProperties {
std::string name;
size_t totalGlobalMem = 0;
int32_t major = 0;
int32_t minor = 0;
int32_t multiProcessorCount = 0;
};

using deviceId_t = int64_t;


} // end namespace devapis
} // end namespace dipu
107 changes: 107 additions & 0 deletions src/turbomind/runtime/device/rawdeviceapis.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (c) 2023, DeepLink.
#pragma once

#include <cstring>

#include "./vendor/vendorapi.h"
#include "./basedef.h"

namespace dipu {

extern devapis::VendorDeviceType VENDOR_TYPE;
namespace devapis {

DIPU_API void initializeVendor();

DIPU_API void finalizeVendor();

DIPU_API deviceId_t current_device();

DIPU_API DIPUDeviceProperties getDeviceProperties(int32_t device_index);

// set current device given device according to id
DIPU_API void setDevice(deviceId_t devId);

DIPU_API void resetDevice(deviceId_t devId = 0);

DIPU_API void syncDevice();

// check last launch succ or not, throw if fail
DIPU_API void checkLastError();

DIPU_API int getDeviceCount();

DIPU_API void getDriverVersion(int* version);

DIPU_API void getRuntimeVersion(int* version);

DIPU_API void createStream(deviceStream_t* stream, bool prior=false);

DIPU_API void destroyStream(deviceStream_t stream);
DIPU_API void destroyStream(deviceStream_t stream, deviceId_t devId);

DIPU_API void releaseStream();

DIPU_API void syncStream(deviceStream_t stream);

DIPU_API bool streamNotNull(deviceStream_t stream);

DIPU_API void streamWaitEvent(deviceStream_t stream, deviceEvent_t event);

// same as query last event status in stream.(every op has a event)
DIPU_API bool isStreamEmpty(deviceStream_t stream);

// =====================
// device event related
// =====================

DIPU_API void createEvent(deviceEvent_t* event);

DIPU_API void destroyEvent(deviceEvent_t event);

DIPU_API void waitEvent(deviceEvent_t event);

DIPU_API void recordEvent(deviceEvent_t event, deviceStream_t stream);

DIPU_API void eventElapsedTime(float *time, deviceEvent_t start, deviceEvent_t end);

DIPU_API EventStatus getEventStatus(deviceEvent_t event);

// =====================
// mem related
// =====================
DIPU_API void mallocHost(void** p, size_t nbytes);

DIPU_API void freeHost(void* p);

DIPU_API OpStatus mallocDevice(void** p, size_t nbytes, bool throwExcepion= true);

DIPU_API void freeDevice(void* p);

DIPU_API bool isPinnedPtr(const void *p);

// (asynchronous) set val
DIPU_API void memSetAsync(const deviceStream_t stream, void* ptr, int val, size_t size);

// (synchronous) copy from device to a device
DIPU_API void memCopyD2D(size_t nbytes, deviceId_t dstDevId, void* dst, deviceId_t srcDevId, const void* src);

// (synchronous) copy from host to a device
DIPU_API void memCopyH2D(size_t nbytes, /*deviceId_t dstDevId,*/ void* dst, /*Host srcDev,*/ const void* src);

// (synchronous) copy from a device to host
DIPU_API void memCopyD2H(size_t nbytes, /*Host dstDev,*/ void* dst, /*deviceId_t srcDevId,*/ const void* src);

// (asynchronous) copy from device to a device
DIPU_API void memCopyD2DAsync(const deviceStream_t stream, size_t nbytes,
deviceId_t dstDevId, void* dst, deviceId_t srcDevId, const void* src);

// (asynchronous) copy from host to a device
DIPU_API void memCopyH2DAsync(const deviceStream_t stream, size_t nbytes,
/*deviceId_t dstDevId,*/ void* dst, /*Host srcDev,*/ const void* src);

// (asynchronous) copy from a device to host
DIPU_API void memCopyD2HAsync(const deviceStream_t stream, size_t nbytes,
/*Host dstDev,*/ void* dst, /*deviceId_t srcDevId,*/ const void* src);
} // end namespace devapis
} // end namespace dipu
Loading