llvm-project/llvm/unittests/Analysis/TensorSpecTest.cpp
Mircea Trofin 5b8dc7c8a5 [mlgo] Introduce an "InteractiveModelRunner"
This is a model runner for ML researchers using environments like
CompilerGym. In such environments, researchers host the compiler and
want to be able to observe the problem space (features) at each decision
step of some optimization pass, at which point the compiler is stopped,
waiting for the host makes a decision and provide an advice back to
the compiler, which then continues its normal operation, and so on.

The InteractiveModelRunner supports this scenario for the feature set
exposed by the compiler at a given time. It uses 2 files - ideally FIFO
pipes - one to pass data to the host, the other to get advices back from
the host. This means this scenario is supported with no special
dependencies. The file creation and deletion is the responsibility of
the host. Hooking up this model evaluator to a MLGO-ed pass is the
responsibilty of the pass author, and subsequent patches will do so for
the current set of mlgo passes, and offer an API to easily "just opt in"
by default when mlgo-ing a new pass.

The data protocol is that of the training logger: the host sees a training
log doled out observation by observation by reading from one of the
files, and passes back its advice as a serialized tensor (i.e. tensor value
memory dump) via the other file.

There are some differences wrt the log seen during training: the
interactive model doesn't currently include the outcome (because it should be
identical to the decision, and it's also not present in the "release"
mode); and partial rewards aren't currently communicated back.

The assumption - just like with the training logger - is that the host
is co-located, thus avoiding any endianness concerns. In a distributed
environment, it is up to the hosting infrastructure to intermediate
that.

Differential Revision: https://reviews.llvm.org/D142642
2023-01-27 17:03:28 -08:00

68 lines
2.3 KiB
C++

//===- TensorSpecTest.cpp - test for TensorSpec ---------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/TensorSpec.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Testing/Support/SupportHelpers.h"
#include "gtest/gtest.h"
using namespace llvm;
extern const char *TestMainArgv0;
TEST(TensorSpecTest, JSONParsing) {
auto Value = json::parse(
R"({"name": "tensor_name",
"port": 2,
"type": "int32_t",
"shape":[1,4]
})");
EXPECT_TRUE(!!Value);
LLVMContext Ctx;
std::optional<TensorSpec> Spec = getTensorSpecFromJSON(Ctx, *Value);
EXPECT_TRUE(Spec);
EXPECT_EQ(*Spec, TensorSpec::createSpec<int32_t>("tensor_name", {1, 4}, 2));
}
TEST(TensorSpecTest, JSONParsingInvalidTensorType) {
auto Value = json::parse(
R"(
{"name": "tensor_name",
"port": 2,
"type": "no such type",
"shape":[1,4]
}
)");
EXPECT_TRUE(!!Value);
LLVMContext Ctx;
auto Spec = getTensorSpecFromJSON(Ctx, *Value);
EXPECT_FALSE(Spec);
}
TEST(TensorSpecTest, TensorSpecSizesAndTypes) {
auto Spec1D = TensorSpec::createSpec<int16_t>("Hi1", {1});
auto Spec2D = TensorSpec::createSpec<int16_t>("Hi2", {1, 1});
auto Spec1DLarge = TensorSpec::createSpec<float>("Hi3", {10});
auto Spec3DLarge = TensorSpec::createSpec<float>("Hi3", {2, 4, 10});
EXPECT_TRUE(Spec1D.isElementType<int16_t>());
EXPECT_FALSE(Spec3DLarge.isElementType<double>());
EXPECT_EQ(Spec1D.getElementCount(), 1U);
EXPECT_EQ(Spec2D.getElementCount(), 1U);
EXPECT_EQ(Spec1DLarge.getElementCount(), 10U);
EXPECT_EQ(Spec3DLarge.getElementCount(), 80U);
EXPECT_EQ(Spec3DLarge.getElementByteSize(), sizeof(float));
EXPECT_EQ(Spec1D.getElementByteSize(), sizeof(int16_t));
}
TEST(TensorSpecTest, PrintValueForDebug) {
std::vector<int32_t> Values{1, 3};
EXPECT_EQ(tensorValueToString(reinterpret_cast<const char *>(Values.data()),
TensorSpec::createSpec<int32_t>("name", {2})),
"1,3");
}