본문 바로가기

dev_AI_framework

E 행렬 기반 forward, backward 연산과 loss, metrics 연산 통합

별도의 모듈로 분리되어 있던 두 연산 종류들을 하나로 통합필요,

불필요한 값 복사, 전달의 최소화를 위해

 

계산 그래프 부분을 담당하는 OpType 의 LOSS 추가 

#pragma once
#include <string>

enum OpType {
    MATMUL = 0,
    ADD = 1,
    RELU = 2,
    SIGMOID = 3,
    TANH = 4,
    FLATTEN = 5,
    CONV2D = 6,
    LOSS = 7
};

struct Shape {
    int rows;
    int cols;
};

// ✅ CNN, RNN, Generic 연산에 공통적으로 사용할 확장 가능한 구조체
struct OpExtraParams {
    int kernel_h = 0;
    int kernel_w = 0;
    int stride_h = 1;
    int stride_w = 1;
    int padding_h = 0;
    int padding_w = 0;
    int input_h = 0;
    int input_w = 0;
    int input_c = 1;
    int output_c = 1;
    int batch_size = 1;

    int time_steps = 0;
    int hidden_size = 0;
    int num_layers = 1;

    bool use_bias = true;
};

// ✅ 수정: extra_params 멤버 포함
struct OpStruct {
    int op_type;
    std::string input_id;
    std::string param_id;
    std::string output_id;
    OpExtraParams extra_params;

    // 기본 생성자
    OpStruct() = default;

    // 확장 생성자 (extra_params 포함)
    OpStruct(int type, std::string in, std::string param, std::string out, OpExtraParams extra = {})
        : op_type(type), input_id(std::move(in)), param_id(std::move(param)), output_id(std::move(out)), extra_params(std::move(extra)) {}
};

 

 

기존의 bindings 코드 내 함수들

  • run_graph_forward_entry : E 행렬 기반 순전파 연산 수행, 단순 예측예 사용 가능
  • run_graph_with_loss_entry : 순전파 연산과 함께 비용함수값 계산, 단순 loss 연산의 추가
  • run_graph_backward_entry : 역전파 연산 수행

 

추가로 E 행렬을 구성하는 값의 추가, Loss function 의 종류별 추가가 아닌 Opstruct 에선 LOSS 선언만 수행, 이후 추가 case 문을 통한 각 loss function 별 연산 수행

 

앞으로 수행해야 할 것으로 가중치 갱신을 위한 optimizer 의 CUDA 내 구현 필요