Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .proj.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ cuda = false
type = "bin"
cuda = false

[targets.import-pt2]
type = "bin"
cuda = false

[targets.substitution-to-dot]
type = "bin"
cuda = false
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ option(FF_BUILD_SP_IZATION_BENCHMARKING "build sp-ization benchmarking" ON)
option(FF_BUILD_ARG_PARSER "build command line argument parser" OFF)
option(FF_BUILD_BIN_COMPILE_MODEL "build compile-model binary" ON)
option(FF_BUILD_BIN_EXPORT_MODEL_ARCH "build export-model-arch utility" ON)
option(FF_BUILD_BIN_IMPORT_PT2 "build import-pt2 utility" ON)
option(FF_BUILD_BIN_RUN_MODEL "build run-model binary" ON)

if (FF_USE_CODE_COVERAGE)
Expand Down
4 changes: 4 additions & 0 deletions bin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ if(FF_BUILD_BIN_EXPORT_MODEL_ARCH)
add_subdirectory(export-model-arch)
endif()

if(FF_BUILD_BIN_IMPORT_PT2)
add_subdirectory(import-pt2)
endif()

if(FF_BUILD_BIN_RUN_MODEL)
add_subdirectory(run-model)
endif()
11 changes: 11 additions & 0 deletions bin/import-pt2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ff_add_executable(
NAME
import-pt2
SRC_PATTERNS
src/*.cc
PRIVATE_INCLUDE
include/
DEPS
utils
pcg
)
51 changes: 51 additions & 0 deletions bin/import-pt2/include/import-pt2/pt2_archive.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#ifndef _FLEXFLOW_BIN_IMPORT_PT2_INCLUDE_IMPORT_PT2_PT2_ARCHIVE_H
#define _FLEXFLOW_BIN_IMPORT_PT2_INCLUDE_IMPORT_PT2_PT2_ARCHIVE_H

#include <cstdint>
#include <map>
#include <nlohmann/json.hpp>
#include <string>

namespace FlexFlow {

/**
* @brief Location of one entry inside a zip file.
*/
struct ZipEntryLocation {
uint16_t compression_method;
uint64_t compressed_size;
uint64_t uncompressed_size;
uint64_t local_header_offset;
};

/**
* @brief Read the central directory of the zip file at \p path (zip64 aware).
*
* @return a map from entry name to its location.
*/
std::map<std::string, ZipEntryLocation>
read_zip_central_directory(std::string const &path);

/**
* @brief Read the contents of a stored (uncompressed) zip entry.
*
* PyTorch's archive writer stores every entry uncompressed, so no inflate
* support is needed. Throws if the entry is compressed.
*/
std::string read_stored_zip_entry(std::string const &path,
std::string const &entry_name,
ZipEntryLocation const &location);

/**
* @brief Read and parse <tt>models/{model_name}.json</tt> (the serialized
* ExportedProgram) from a <tt>torch.export.save</tt> .pt2 archive.
*
* The archive's top-level directory is not fixed (PyTorch names it after the
* file), so any single top-level directory is accepted.
*/
nlohmann::json read_pt2_model_json(std::string const &pt2_path,
std::string const &model_name = "model");

} // namespace FlexFlow

#endif
37 changes: 37 additions & 0 deletions bin/import-pt2/include/import-pt2/pt2_to_computation_graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef _FLEXFLOW_BIN_IMPORT_PT2_INCLUDE_IMPORT_PT2_PT2_TO_COMPUTATION_GRAPH_H
#define _FLEXFLOW_BIN_IMPORT_PT2_INCLUDE_IMPORT_PT2_PT2_TO_COMPUTATION_GRAPH_H

#include "pcg/computation_graph.dtg.h"
#include <nlohmann/json.hpp>
#include <stdexcept>

namespace FlexFlow {

/**
* @brief Thrown when a .pt2 graph uses something the importer does not
* support (an unknown ATen op, dynamic shapes, unsupported arguments, ...).
*/
struct Pt2ImportError : public std::runtime_error {
using std::runtime_error::runtime_error;
};

/**
* @brief Rebuild a serialized torch.export ExportedProgram (the contents of
* <tt>models/model.json</tt> in a .pt2 archive) as a FlexFlow
* ComputationGraph, using ComputationGraphBuilder.
*
* Each ATen node becomes the matching builder call. Parameters and buffers
* are not imported as tensors; they are implied by the op that owns them (e.g.
* <tt>aten.conv2d</tt>'s weight determines the conv's output channels and
* kernel size, and its bias determines <tt>use_bias</tt>). Weight values are
* not imported.
*
* After every op, the shape FlexFlow infers is checked against the shape
* recorded in the .pt2. Any mismatch throws Pt2ImportError naming the node.
*/
ComputationGraph
computation_graph_from_pt2_model_json(nlohmann::json const &model);

} // namespace FlexFlow

#endif
28 changes: 28 additions & 0 deletions bin/import-pt2/index.dox
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace FlexFlow {
/**

@page import-pt2 import-pt2

\brief Imports a PyTorch model saved with <tt>torch.export.save()</tt> (a <tt>.pt2</tt> file) as a FlexFlow ComputationGraph, and outputs it as JSON (see \ref file-format) or DOT.

The <tt>.pt2</tt> archive is read directly (no libtorch dependency). Each ATen op in the exported graph is rebuilt with ComputationGraphBuilder; parameters and buffers are implied by the ops that use them. Weight values are not imported.

Export a model from PyTorch:

\verbatim
ep = torch.export.export(model.eval(), (example_input,))
torch.export.save(ep, "model.pt2")
\endverbatim

Then build and run \c import-pt2 from the root of the %FlexFlow %Train repository:

\verbatim
$ proj cmake # if you haven't already
$ proj build
$ ./build/normal/bin/import-pt2/import-pt2 model.pt2 > model.json
$ ./build/normal/bin/import-pt2/import-pt2 --dot model.pt2 > model.dot
\endverbatim

Unsupported ops, dynamic shapes, and unsupported op arguments are reported as errors naming the offending node.
*/
}
77 changes: 77 additions & 0 deletions bin/import-pt2/src/import-pt2/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "import-pt2/pt2_archive.h"
#include "import-pt2/pt2_to_computation_graph.h"
#include "pcg/computation_graph.h"
#include "pcg/file_format/v1/v1_computation_graph.h"
#include "utils/cli/cli_get_help_message.h"
#include "utils/cli/cli_parse.h"
#include "utils/cli/cli_parse_result.h"
#include "utils/cli/cli_spec.h"
#include <iostream>

using namespace ::FlexFlow;

int main(int argc, char **argv) {
CLISpec cli;

CLIArgumentKey key_help = cli_add_help_flag(cli);

CLIArgumentKey key_dot = cli.add_flag(CLIFlagSpec{
/*long_flag=*/"dot",
/*short_flag=*/std::nullopt,
/*description=*/
"output a dot representation of the imported computation graph "
"instead of JSON",
});

CLIArgumentKey key_pt2_path =
cli.add_positional_argument(CLIPositionalArgumentSpec{
/*name=*/"pt2_file",
/*choices=*/std::nullopt,
/*description=*/"a .pt2 file written by torch.export.save()",
});

assert(argc >= 1);
std::string prog_name = argv[0];

CLIParseResult parsed = ({
tl::expected<CLIParseResult, std::string> result =
cli_parse(cli, argc, argv);
if (!result.has_value()) {
std::cerr << cli_get_help_message(prog_name, cli);
std::cerr << std::endl;
std::cerr << "error: " << result.error() << std::endl;
return 1;
}

result.value();
});

if (cli_get_flag(parsed, key_help)) {
std::cerr << cli_get_help_message(prog_name, cli);
return 1;
}

std::string pt2_path = cli_get_positional_argument(parsed, key_pt2_path);
bool dot = cli_get_flag(parsed, key_dot);

ComputationGraph cg = ({
std::optional<ComputationGraph> result;
try {
result =
computation_graph_from_pt2_model_json(read_pt2_model_json(pt2_path));
} catch (std::exception const &e) {
std::cerr << "error: " << e.what() << std::endl;
return 1;
}
result.value();
});

if (dot) {
std::cout << as_dot(cg) << std::endl;
} else {
nlohmann::json json_output = to_v1(cg);
std::cout << json_output.dump(2) << std::endl;
}

return 0;
}
Loading