<commit_before>#ifndef UTENSOR_TENSOR_H
#define UTENSOR_TENSOR_H

#include "uTensor/util/uTensor_util.hpp"
#include <initializer_list>
#include <iostream>
#include <memory>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <limits>

// enum class DType : char {
//   uint8,
//   int8,
//   uint16,
//   int32,
//   flt,
//   dbl,
// };

class Tensor;
class TensorIdxImporter;
typedef std::string TName;
typedef std::string OpName;
typedef std::vector<TName> TNameList;
typedef std::shared_ptr<Tensor> S_TENSOR;
typedef std::vector<S_TENSOR> S_TList;

class TensorShape: public std::vector<uint32_t> {
    public:
        using std::vector<uint32_t>::vector;

};

class uTensor {
public:
 virtual void inFocus(){};
 virtual void deFocus(){};
 virtual const std::string& getName() const;
 virtual void setName(std::string _name);

 virtual ~uTensor() = 0;
private:
 std::string name;

};

//inline uTensor::~uTensor() {}
class TensorBase {
 public:
  TensorShape shape;
  void* data;
  uint32_t total_size;
  uint32_t cache_size;

  void initialize(const TensorShape& vec);
  void allocate(uint8_t unit_size);

  ~TensorBase();
};

class Tensor : public uTensor {
  virtual void* read(size_t offset, size_t ele);
  virtual void* write(size_t offset, size_t ele);
  Tensor(const Tensor&);
  Tensor& operator=(const Tensor&);

 protected:
  std::shared_ptr<TensorBase> s;  // short for states
 public:
  Tensor();

  // returns how far a given dimension is apart
  size_t getStride(size_t dim_index); 

  virtual void init(const TensorShape& v); 

  virtual void init(const TensorShape& v, const void* data); 

  virtual void resize(const TensorShape& v); 

  const TensorShape& getShape(void) const; 

  uint32_t getSize(void); 

  virtual uint16_t unit_size(void); 

  uint32_t getSize_in_bytes(void); 

  // returns the number of dimensions in the tensor
  size_t getDim(void); 

  template <class T>
  const T* read(size_t offset, size_t ele) {
    return (const T*)read(offset, ele);
  }

  template <class T>
  T* write(size_t offset, size_t ele) {
    return (T*)write(offset, ele);
  }

  ~Tensor(); 
};

template<class T>
class BinaryTensor : public Tensor {
  public:
  BinaryTensor(const TensorShape& v, const T* g) : Tensor() {
    Tensor::init(v, g);
  }

  virtual uint16_t unit_size(void) override {
    return sizeof(T);
  }

  virtual void* read(size_t offset, size_t ele) override {
    if (ele > s->total_size) {
        ERR_EXIT("data overflow");
    }
    return (void *)((T*)s->data + offset);
  }

  virtual void* write(size_t offset, size_t ele) override {
    return nullptr;
  }
  ~BinaryTensor() {
    s->data = nullptr;
  }

 private:
  BinaryTensor(const BinaryTensor&);
  BinaryTensor& operator=(const BinaryTensor&);
};

template <class T>
class RamTensor : public Tensor {
  // need deep copy
 public:
  //RamTensor(TName _name) : Tensor(_name) {}
  RamTensor() {};

  RamTensor(std::initializer_list<uint32_t> l) {
    TensorShape v;
    for (auto i : l) {
      v.push_back(i);
    }

    Tensor::init(v);
  }

  RamTensor(const TensorShape& v) : Tensor() {
    Tensor::init(v);
  }

  // PRE:      l, initization list, specifying the element/dimension
  // POST:     When a degenerative index is supplied, the pointer
  //          lowest specified dimension is returned.
  //          Otherwise, return the pointer to the specific element.
  virtual void* read(size_t offset, size_t ele) override {
    if (ele > s->total_size) {
        ERR_EXIT("data overflow");
    }
    return (void *)((T*)s->data + offset);
  }
  virtual void* write(size_t offset, size_t ele) override {
    if (ele > s->total_size) {
        ERR_EXIT("data overflow");
    }
    return (void*)((T*)s->data + offset);
  }


  // virtual void* read(size_t offset, size_t ele) override{};
  virtual uint16_t unit_size(void) override {
    return sizeof(T);
  }
  ~RamTensor() {}
 private:
  RamTensor(const RamTensor&);
  RamTensor& operator=(const RamTensor&);

};

template <typename Tin, typename Tout>
Tensor* TensorCast(Tensor* input) {
  Tensor* output = new RamTensor<Tout>(input->getShape());
  const Tin* inputPrt = input->read<Tin>(0, 0);
  Tout* outputPrt = output->write<Tout>(0, 0);

  for (uint32_t i = 0; i < input->getSize(); i++) {
    outputPrt[i] = static_cast<Tout>(inputPrt[i]);
  }

  return output;
}

template <typename T>
Tensor* TensorConstant(const TensorShape& shape, T c) {
  Tensor* output = new RamTensor<T>(shape);
  T* outPrt = output->write<T>(0, 0);

  for (uint32_t i = 0; i < output->getSize(); i++) {
    outPrt[i] = c;
  }

  return output;
}

template <typename T>
Tensor* TensorConstant(std::initializer_list<uint32_t> l, T c) {<commit_msg>initial WrappedRamTensor : RamTensor implementation<commit_after>#ifndef UTENSOR_TENSOR_H
#define UTENSOR_TENSOR_H

#include "uTensor/util/uTensor_util.hpp"
#include <initializer_list>
#include <iostream>
#include <memory>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <limits>

// enum class DType : char {
//   uint8,
//   int8,
//   uint16,
//   int32,
//   flt,
//   dbl,
// };

class Tensor;
class TensorIdxImporter;
typedef std::string TName;
typedef std::string OpName;
typedef std::vector<TName> TNameList;
typedef std::shared_ptr<Tensor> S_TENSOR;
typedef std::vector<S_TENSOR> S_TList;

class TensorShape: public std::vector<uint32_t> {
    public:
        using std::vector<uint32_t>::vector;

};

class uTensor {
public:
 virtual void inFocus(){};
 virtual void deFocus(){};
 virtual const std::string& getName() const;
 virtual void setName(std::string _name);

 virtual ~uTensor() = 0;
private:
 std::string name;

};

//inline uTensor::~uTensor() {}
class TensorBase {
 public:
  TensorShape shape;
  void* data;
  uint32_t total_size;
  uint32_t cache_size;

  void initialize(const TensorShape& vec);
  void allocate(uint8_t unit_size);

  ~TensorBase();
};

class Tensor : public uTensor {
  virtual void* read(size_t offset, size_t ele);
  virtual void* write(size_t offset, size_t ele);
  Tensor(const Tensor&);
  Tensor& operator=(const Tensor&);

 protected:
  std::shared_ptr<TensorBase> s;  // short for states
 public:
  Tensor();

  // returns how far a given dimension is apart
  size_t getStride(size_t dim_index); 

  virtual void init(const TensorShape& v); 

  virtual void init(const TensorShape& v, const void* data); 

  virtual void resize(const TensorShape& v); 

  const TensorShape& getShape(void) const; 

  uint32_t getSize(void); 

  virtual uint16_t unit_size(void); 

  uint32_t getSize_in_bytes(void); 

  // returns the number of dimensions in the tensor
  size_t getDim(void); 

  template <class T>
  const T* read(size_t offset, size_t ele) {
    return (const T*)read(offset, ele);
  }

  template <class T>
  T* write(size_t offset, size_t ele) {
    return (T*)write(offset, ele);
  }

  ~Tensor(); 
};

template<class T>
class BinaryTensor : public Tensor {
  public:
  BinaryTensor(const TensorShape& v, const T* g) : Tensor() {
    Tensor::init(v, g);
  }

  virtual uint16_t unit_size(void) override {
    return sizeof(T);
  }

  virtual void* read(size_t offset, size_t ele) override {
    if (ele > s->total_size) {
        ERR_EXIT("data overflow");
    }
    return (void *)((T*)s->data + offset);
  }

  virtual void* write(size_t offset, size_t ele) override {
    return nullptr;
  }
  ~BinaryTensor() {
    s->data = nullptr;
  }

 private:
  BinaryTensor(const BinaryTensor&);
  BinaryTensor& operator=(const BinaryTensor&);
};

template <class T>
class RamTensor : public Tensor {
  // need deep copy
 protected:
    using Tensor::s;
 public:
  //RamTensor(TName _name) : Tensor(_name) {}
  RamTensor() {};

  RamTensor(std::initializer_list<uint32_t> l) {
    TensorShape v;
    for (auto i : l) {
      v.push_back(i);
    }

    Tensor::init(v);
  }

  RamTensor(const TensorShape& v) : Tensor() {
    Tensor::init(v);
  }

  // PRE:      l, initization list, specifying the element/dimension
  // POST:     When a degenerative index is supplied, the pointer
  //          lowest specified dimension is returned.
  //          Otherwise, return the pointer to the specific element.
  virtual void* read(size_t offset, size_t ele) override {
    if (ele > s->total_size) {
        ERR_EXIT("data overflow");
    }
    return (void *)((T*)s->data + offset);
  }
  virtual void* write(size_t offset, size_t ele) override {
    if (ele > s->total_size) {
        ERR_EXIT("data overflow");
    }
    return (void*)((T*)s->data + offset);
  }


  // virtual void* read(size_t offset, size_t ele) override{};
  virtual uint16_t unit_size(void) override {
    return sizeof(T);
  }
  ~RamTensor() {}
 private:
  RamTensor(const RamTensor&);
  RamTensor& operator=(const RamTensor&);

};


// application owns the pointer
// the printer may be modified outside of the application
template <class T>
class WrappedRamTensor : public RamTensor<T> {

 protected:
  using RamTensor<T>::s;

 public:
  WrappedRamTensor() {};

  WrappedRamTensor(std::initializer_list<uint32_t> l, T* ptr) {
    std::vector<uint32_t> v;
    for (auto i : l) {
      v.push_back(i);
    }
    
    Tensor::init(v, ptr);
  }

  void setPointer(void* ptr) {
    s->data = ptr;
  }

  T* getPointer(void) {
    return (T*) s->data;
  }

  ~WrappedRamTensor() {
    s->data = nullptr;
  }

 private:
  WrappedRamTensor(const WrappedRamTensor&);
  WrappedRamTensor& operator=(const WrappedRamTensor&);

};

template <typename Tin, typename Tout>
Tensor* TensorCast(Tensor* input) {
  Tensor* output = new RamTensor<Tout>(input->getShape());
  const Tin* inputPrt = input->read<Tin>(0, 0);
  Tout* outputPrt = output->write<Tout>(0, 0);

  for (uint32_t i = 0; i < input->getSize(); i++) {
    outputPrt[i] = static_cast<Tout>(inputPrt[i]);
  }

  return output;
}

template <typename T>
Tensor* TensorConstant(const TensorShape& shape, T c) {
  Tensor* output = new RamTensor<T>(shape);
  T* outPrt = output->write<T>(0, 0);

  for (uint32_t i = 0; i < output->getSize(); i++) {
    outPrt[i] = c;
  }

  return output;
}

template <typename T>
Tensor* TensorConstant(std::initializer_list<uint32_t> l, T c) {
{'fraction_non_alphanumeric': 0.13644859813084112, 'fraction_numerical': 0.008657156910969011, 'mean_word_length': 3.7086614173228347, 'pattern_counts': {'":': 0, '<': 68, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6577810', 'n_tokens_mistral': 3646, 'n_tokens_neox': 3460, 'n_words': 941}