Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FSU Test & Verification on CPU #2837

Open
2 tasks
DonghakPark opened this issue Dec 24, 2024 · 2 comments
Open
2 tasks

FSU Test & Verification on CPU #2837

DonghakPark opened this issue Dec 24, 2024 · 2 comments

Comments

@DonghakPark
Copy link
Member

FSU CPU side Test & Verification at Inference

  • Inference test (Fully Connected Layer)
  • test swap mem_usage ( each lookahead )

work with @SeoHyungjun

we will update test result & issues on this issue

@DonghakPark
Copy link
Member Author

DonghakPark commented Dec 24, 2024

@SeoHyungjun My test Model is here

std::vector<LayerHandle> createGraph() {
  using ml::train::createLayer;

  std::vector<LayerHandle> layers;

  layers.push_back(createLayer(
    "input", {withKey("name", "input0"), withKey("input_shape", "1:1:320")}));

  layers.push_back(createLayer("fully_connected",
                               {withKey("unit", 150),
                                withKey("weight_initializer", "xavier_uniform"),
                                withKey("bias_initializer", "zeros")}));

  for (int i = 0; i < 100; i++) {
    layers.push_back(createLayer(
      "fully_connected",
      {withKey("unit", 5000), withKey("weight_initializer", "xavier_uniform"),
       withKey("bias_initializer", "zeros")}));
  }

  layers.push_back(createLayer("fully_connected",
                               {withKey("unit", 100),
                                withKey("weight_initializer", "xavier_uniform"),
                                withKey("bias_initializer", "zeros")}));

  return layers;
}

@DonghakPark
Copy link
Member Author

DonghakPark commented Dec 27, 2024

@SeoHyungjun Please check below application code

// SPDX-License-Identifier: Apache-2.0
/**
 * Copyright (C) 2024 Jijoong Moon <[email protected]>
 *
 * @file   main.cpp
 * @date   10 Dec 2024
 * @brief  Test Application for Asynch FSU
 * @see    https://github.com/nnstreamer/nntrainer
 * @author Jijoong Moon <[email protected]>
 * @bug    No known bugs except for NYI items
 */
#include <array>
#include <chrono>
#include <ctime>
#include <iostream>
#include <memory>
#include <sstream>
#include <vector>

#include <layer.h>
#include <model.h>
#include <optimizer.h>

#ifdef PROFILE
#include <profiler.h>
#endif

using LayerHandle = std::shared_ptr<ml::train::Layer>;
using ModelHandle = std::unique_ptr<ml::train::Model>;

/**
 * @brief make "key=value" from key and value
 *
 * @tparam T type of a value
 * @param key key
 * @param value value
 * @return std::string with "key=value"
 */
template <typename T>
static std::string withKey(const std::string &key, const T &value) {
  std::stringstream ss;
  ss << key << "=" << value;
  return ss.str();
}

template <typename T>
static std::string withKey(const std::string &key,
                           std::initializer_list<T> value) {
  if (std::empty(value)) {
    throw std::invalid_argument("empty data cannot be converted");
  }

  std::stringstream ss;
  ss << key << "=";

  auto iter = value.begin();
  for (; iter != value.end() - 1; ++iter) {
    ss << *iter << ',';
  }
  ss << *iter;

  return ss.str();
}

/**
 * @brief Create network
 *
 * @return vector of layers that contain full graph of asynch
 */
std::vector<LayerHandle> createGraph() {
  using ml::train::createLayer;

  std::vector<LayerHandle> layers;

  layers.push_back(createLayer(
    "input", {withKey("name", "input0"), withKey("input_shape", "1:1024:1440")}));

  for (int i = 0; i < 100; i++) {
    layers.push_back(createLayer(
      "fully_connected",
      {withKey("unit", 1440), withKey("weight_initializer", "xavier_uniform"),
       withKey("bias_initializer", "zeros")}));
  }
  layers.push_back(createLayer(
        "fully_connected",
        {withKey("unit", 100), withKey("weight_initializer", "xavier_uniform"),
         withKey("bias_initializer", "zeros")}));
  return layers;
}

ModelHandle create() {
  ModelHandle model = ml::train::createModel(ml::train::ModelType::NEURAL_NET,
                                             {withKey("loss", "mse")});

  for (auto &layer : createGraph()) {
    model->addLayer(layer);
  }

  return model;
}

void createAndRun(unsigned int epochs, unsigned int batch_size
                  ) {


  ModelHandle model = create();
  model->setProperty({withKey("batch_size", batch_size),
                      withKey("epochs", epochs), withKey("memory_swap", "true"),
                      // withKey("memory_swap_lookahead", "1"),
                      withKey("model_tensor_type", "FP16-FP16")});

  auto optimizer = ml::train::createOptimizer("sgd", {"learning_rate=0.001"});
  model->setOptimizer(std::move(optimizer));

  model->compile(ml::train::ExecutionMode::INFERENCE);
  model->initialize(ml::train::ExecutionMode::INFERENCE);

  // model->save("simplefc_weight_fp16_fp16_100.bin",
  //             ml::train::ModelFormat::MODEL_FORMAT_BIN);
  model->load("./simplefc_weight_fp16_fp16_100.bin");

  uint feature_size = 1*1024*1440;

  float input[feature_size];
  float label[1];

  for (uint j = 0; j < feature_size; ++j)
    input[j] = j;

  std::vector<float *> in;
  std::vector<float *> l;
  std::vector<float *> answer;

  in.push_back(input);
  l.push_back(label);

  auto start = std::chrono::system_clock::now();
  std::time_t start_time = std::chrono::system_clock::to_time_t(start);
  std::cout << "started computation at " << std::ctime(&start_time)
            << std::endl;

  answer = model->inference(1, in, l);

  auto end = std::chrono::system_clock::now();
  std::chrono::duration<double> elapsed_seconds = end - start;
  std::time_t end_time = std::chrono::system_clock::to_time_t(end);
  std::cout << "finished computation at " << std::ctime(&end_time)
            << "elapsed time: " << elapsed_seconds.count() << "s\n";

  in.clear();
  l.clear();

  std::cout << "done" << std::endl;
}

int main(int argc, char *argv[]) {

#ifdef PROFILE
  auto listener =
    std::make_shared<nntrainer::profile::GenericProfileListener>();
  nntrainer::profile::Profiler::Global().subscribe(listener);
#endif

  uint batch_size = 1;
  uint epoch = 1;

  createAndRun(epoch, batch_size);

#ifdef PROFILE
  std::cout << *listener;
#endif

  return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant