Skip to content

Instantly share code, notes, and snippets.

@reyoung
Last active May 28, 2018 04:25
Show Gist options
  • Save reyoung/9f5b7d452e00f28aeb17d6d6a2ec45b2 to your computer and use it in GitHub Desktop.
Save reyoung/9f5b7d452e00f28aeb17d6d6a2ec45b2 to your computer and use it in GitHub Desktop.
#include "c_api_debugger.h"
#include <iostream>
#include <stdint.h>
namespace paddle {
namespace capi {
namespace debugger {
static void PrintArg(std::ostream &os, paddle_arguments arg, uint64_t id,
bool only_shape, size_t seq_level);
void PrintArgs(std::ostream &os, paddle_arguments arg, bool only_shape,
std::vector<size_t> seq_level) {
uint64_t size;
paddle_arguments_get_size(arg, &size);
os << "Arguments with size=" << size << "\n";
if (seq_level.size() != size) {
throw "Sequece level size must be as same as args size";
}
for (uint64_t i = 0; i < size; ++i) {
PrintArg(os, arg, i, only_shape, seq_level.at(i));
}
}
void PrintMat(std::ostream &os, paddle_matrix mat, bool only_shape,
const char *line_start) {
uint64_t h, w;
auto err = paddle_matrix_get_shape(mat, &h, &w);
if (err == kPD_NULLPTR) {
os << line_start << "Matrix(NULL)\n";
return;
}
os << line_start << "Matrix(" << h << ", " << w << ")\n";
if (only_shape) {
return;
}
paddle_real *buffer = new paddle_real[h * w];
paddle_matrix_get_value(mat, buffer);
for (uint64_t i = 0; i < h; ++i) {
os << line_start << "\t";
for (uint64_t j = 0; j < w; ++j) {
os << buffer[i * w + j];
if (j + 1 != w) {
os << ", ";
}
}
os << "\n";
}
delete buffer;
}
void PrintIVec(std::ostream &os, paddle_ivector ivec, bool only_shape,
const char *line_start) {
uint64_t size;
auto err = paddle_ivector_get_size(ivec, &size);
if (err == kPD_NULLPTR) {
os << line_start << "IVec(NULL)\n";
return;
}
os << line_start << "IVec(" << size << ")\n";
if (only_shape)
return;
int *buffer;
paddle_ivector_get(ivec, &buffer);
os << line_start << "\t";
for (uint64_t i = 0; i < size; ++i) {
os << buffer[i];
if (i + 1 != size) {
os << ", ";
}
}
os << "\n";
}
void PrintArg(std::ostream &os, paddle_arguments arg, uint64_t id,
bool only_shape, size_t seq_level) {
auto mat = paddle_matrix_create_none();
paddle_arguments_get_value(arg, id, mat);
PrintMat(os, mat, only_shape, "\tVALUES: ");
paddle_matrix_destroy(mat);
auto ivec = paddle_ivector_create_none();
paddle_arguments_get_ids(arg, id, ivec);
PrintIVec(os, ivec, only_shape, "\tIDS: ");
if (seq_level > 0) {
paddle_arguments_get_sequence_start_pos(arg, id, 0, ivec);
PrintIVec(os, ivec, only_shape, "\tSeqPos: ");
if (seq_level > 1) {
paddle_arguments_get_sequence_start_pos(arg, id, 1, ivec);
PrintIVec(os, ivec, only_shape, "\tSubSeqPos: ");
}
}
paddle_ivector_destroy(ivec);
}
} // namespace debugger
} // namespace capi
} // namespace paddle
#pragma once
#include "paddle/capi.h"
#include <iosfwd>
#include <vector>
namespace paddle {
namespace capi {
namespace debugger {
// seq_level = 0 means no sequence
// seq_level = 1 means a sequence of feature
// seq_level = 2 means a sequence of sequence.
// seq_level must be as same as arg size. e.g.,
// PrintArgs(os, arg, false, {0, 1, 1});
extern void PrintArgs(std::ostream &os, paddle_arguments arg, bool only_shape,
std::vector<size_t> seq_level);
extern void PrintMat(std::ostream &os, paddle_matrix mat, bool only_shape,
const char *line_start = "");
extern void PrintIVec(std::ostream &os, paddle_ivector ivec, bool only_shape,
const char *line_start = "");
} // namespace debugger
} // namespace capi
} // namespace paddle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment