<commit_before>// Copyright 2019 Intel Corporation.

#pragma once

#include <cstring>
#include <functional>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>

#include "plaidml/core/ffi.h"

namespace plaidml {

namespace ffi {

inline std::string str(plaidml_string* ptr) {
  std::string ret{plaidml_string_ptr(ptr)};
  plaidml_string_free(ptr);
  return ret;
}

template <typename T, typename F, typename... Args>
T call(F fn, Args... args) {
  plaidml_error err;
  auto ret = fn(&err, args...);
  if (err.code) {
    throw std::runtime_error(str(err.msg));
  }
  return ret;
}

template <typename F, typename... Args>
void call_void(F fn, Args... args) {
  plaidml_error err;
  fn(&err, args...);
  if (err.code) {
    throw std::runtime_error(str(err.msg));
  }
}

}  // namespace ffi

namespace details {

struct Deleter {
  void operator()(plaidml_buffer* ptr) { ffi::call_void(plaidml_buffer_free, ptr); }
  void operator()(plaidml_integers* ptr) { ffi::call_void(plaidml_integers_free, ptr); }
  void operator()(plaidml_shape* ptr) { ffi::call_void(plaidml_shape_free, ptr); }
  void operator()(plaidml_view* ptr) { ffi::call_void(plaidml_view_free, ptr); }
};

template <typename T>
inline std::shared_ptr<T> make_ptr(T* ptr) {
  return std::shared_ptr<T>(ptr, Deleter{});
}

}  // namespace details

///
/// Initializes the PlaidML Core API.
///
inline void init() {  //
  ffi::call_void(plaidml_init);
}

///
/// \defgroup core_objects Objects
///

///
/// \ingroup core_objects
/// \enum DType
/// Defines the set of supported element types in a Tensor.
///
enum class DType {
  INVALID = PLAIDML_DATA_INVALID,
  BOOLEAN = PLAIDML_DATA_BOOLEAN,
  INT8 = PLAIDML_DATA_INT8,
  UINT8 = PLAIDML_DATA_UINT8,
  INT16 = PLAIDML_DATA_INT16,
  UINT16 = PLAIDML_DATA_UINT16,
  INT32 = PLAIDML_DATA_INT32,
  UINT32 = PLAIDML_DATA_UINT32,
  INT64 = PLAIDML_DATA_INT64,
  UINT64 = PLAIDML_DATA_UINT64,
  BFLOAT16 = PLAIDML_DATA_BFLOAT16,
  FLOAT16 = PLAIDML_DATA_FLOAT16,
  FLOAT32 = PLAIDML_DATA_FLOAT32,
  FLOAT64 = PLAIDML_DATA_FLOAT64,
};

inline const char* to_string(DType dtype) {
  switch (dtype) {
    case DType::BOOLEAN:
      return "bool";
    case DType::INT8:
      return "int8";
    case DType::UINT8:
      return "uint8";
    case DType::INT16:
      return "int16";
    case DType::UINT16:
      return "uint16";
    case DType::INT32:
      return "int32";
    case DType::UINT32:
      return "uint32";
    case DType::INT64:
      return "uint64";
    case DType::BFLOAT16:
      return "bfloat16";
    case DType::FLOAT16:
      return "float16";
    case DType::FLOAT32:<commit_msg>fix string conversion of DType enums (#984)<commit_after>// Copyright 2019 Intel Corporation.

#pragma once

#include <cstring>
#include <functional>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>

#include "plaidml/core/ffi.h"

namespace plaidml {

namespace ffi {

inline std::string str(plaidml_string* ptr) {
  std::string ret{plaidml_string_ptr(ptr)};
  plaidml_string_free(ptr);
  return ret;
}

template <typename T, typename F, typename... Args>
T call(F fn, Args... args) {
  plaidml_error err;
  auto ret = fn(&err, args...);
  if (err.code) {
    throw std::runtime_error(str(err.msg));
  }
  return ret;
}

template <typename F, typename... Args>
void call_void(F fn, Args... args) {
  plaidml_error err;
  fn(&err, args...);
  if (err.code) {
    throw std::runtime_error(str(err.msg));
  }
}

}  // namespace ffi

namespace details {

struct Deleter {
  void operator()(plaidml_buffer* ptr) { ffi::call_void(plaidml_buffer_free, ptr); }
  void operator()(plaidml_integers* ptr) { ffi::call_void(plaidml_integers_free, ptr); }
  void operator()(plaidml_shape* ptr) { ffi::call_void(plaidml_shape_free, ptr); }
  void operator()(plaidml_view* ptr) { ffi::call_void(plaidml_view_free, ptr); }
};

template <typename T>
inline std::shared_ptr<T> make_ptr(T* ptr) {
  return std::shared_ptr<T>(ptr, Deleter{});
}

}  // namespace details

///
/// Initializes the PlaidML Core API.
///
inline void init() {  //
  ffi::call_void(plaidml_init);
}

///
/// \defgroup core_objects Objects
///

///
/// \ingroup core_objects
/// \enum DType
/// Defines the set of supported element types in a Tensor.
///
enum class DType {
  INVALID = PLAIDML_DATA_INVALID,
  BOOLEAN = PLAIDML_DATA_BOOLEAN,
  INT8 = PLAIDML_DATA_INT8,
  UINT8 = PLAIDML_DATA_UINT8,
  INT16 = PLAIDML_DATA_INT16,
  UINT16 = PLAIDML_DATA_UINT16,
  INT32 = PLAIDML_DATA_INT32,
  UINT32 = PLAIDML_DATA_UINT32,
  INT64 = PLAIDML_DATA_INT64,
  UINT64 = PLAIDML_DATA_UINT64,
  BFLOAT16 = PLAIDML_DATA_BFLOAT16,
  FLOAT16 = PLAIDML_DATA_FLOAT16,
  FLOAT32 = PLAIDML_DATA_FLOAT32,
  FLOAT64 = PLAIDML_DATA_FLOAT64,
};

inline const char* to_string(DType dtype) {
  switch (dtype) {
    case DType::BOOLEAN:
      return "bool";
    case DType::INT8:
      return "int8";
    case DType::UINT8:
      return "uint8";
    case DType::INT16:
      return "int16";
    case DType::UINT16:
      return "uint16";
    case DType::INT32:
      return "int32";
    case DType::UINT32:
      return "uint32";
    case DType::INT64:
      return "int64";
    case DType::UINT64:
      return "uint64";
    case DType::BFLOAT16:
      return "bfloat16";
    case DType::FLOAT16:
      return "float16";
    case DType::FLOAT32:
{'fraction_non_alphanumeric': 0.13624437781109444, 'fraction_numerical': 0.03204647676161919, 'mean_word_length': 3.752448797862867, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '5481540', 'n_tokens_mistral': 2148, 'n_tokens_neox': 2024, 'n_words': 449}