Skip to content

Commit

Permalink
[src] Various fixes w.r.t memory compression; cosmetic fixes too.
Browse files Browse the repository at this point in the history
  • Loading branch information
danpovey committed Jan 29, 2018
1 parent 5dbfe97 commit 46cdd54
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 41 deletions.
28 changes: 15 additions & 13 deletions src/nnet3/nnet-analyze.cc
Expand Up @@ -388,7 +388,7 @@ void ComputeCommandAttributes(
vars.RecordAccessForSubmatrix(c.arg1, kReadWriteAccess, &attr);
break;
}
case kUncompressMatrix: {
case kDecompressMatrix: {
vars.RecordAccessForSubmatrix(c.arg1, kWriteAccess, &attr);
break;
}
Expand Down Expand Up @@ -656,7 +656,11 @@ void ComputationChecker::CheckComputationUndefined() const {
<< a_.variables.DescribeVariable(v) << " is never used.";
}
} else {
if (accesses[0].access_type != kWriteAccess)
// It's OK if part of a matrix is compressed, that is undefined;
// likely that part won't be referred to when we uncompress.
if (accesses[0].access_type != kWriteAccess &&
!(computation_.commands[accesses[0].command_index].command_type ==
kCompressMatrix))
KALDI_ERR << "Variable " << v << " == "
<< a_.variables.DescribeVariable(v)
<< " is read before it is written to";
Expand Down Expand Up @@ -738,7 +742,7 @@ void ComputationChecker::CheckComputationCompression() const {
int32 command_index = access.command_index;
const NnetComputation::Command &command =
computation_.commands[command_index];
if (command.command_type == kUncompressMatrix) {
if (command.command_type == kDecompressMatrix) {
// check that the previous access to this matrix was a compression
// command.
KALDI_ASSERT(
Expand All @@ -751,7 +755,7 @@ void ComputationChecker::CheckComputationCompression() const {
// command.
int32 next_command_index = accesses.accesses[a+1].command_index;
KALDI_ASSERT(computation_.commands[next_command_index].command_type ==
kUncompressMatrix &&
kDecompressMatrix &&
command_index < middle_command &&
next_command_index > middle_command);
if (command.alpha == 0.0) {
Expand Down Expand Up @@ -1042,7 +1046,7 @@ void ComputationChecker::CheckComputationIndexes() const {
KALDI_ERR << "Invalid alpha in kCompressMatrix command.";
break;
}
case kUncompressMatrix: {
case kDecompressMatrix: {
if (c.arg1 < 1 || c.arg1 >= num_submatrices ||
!computation_.IsWholeMatrix(c.arg1))
KALDI_ERR << "submatrix index out of range or invalid";
Expand Down Expand Up @@ -1445,13 +1449,11 @@ int64 GetMaxMemoryUse(const NnetComputation &computation) {
this_num_bytes = static_cast<int64>(sizeof(BaseFloat)) *
submat_info.num_rows * submat_info.num_cols;

if (c.arg2 >= static_cast<int32>(kCompressedMatrixInt8) &&
c.arg2 <= static_cast<int32>(kCompressedMatrixUint16)) {
this_compressed_num_bytes =
((c.arg2 == static_cast<int32>(kCompressedMatrixInt8) ||
c.arg2 == static_cast<int32>(kCompressedMatrixUint8)) ?
1 : 2) * submat_info.num_rows * submat_info.num_cols;
}
this_compressed_num_bytes =
((c.arg2 == static_cast<int32>(kCompressedMatrixInt8) ||
c.arg2 == static_cast<int32>(kCompressedMatrixUint8)) ?
1 : 2) * static_cast<int64>(submat_info.num_rows) *
submat_info.num_cols;
}
switch (c.command_type) {
case kAllocMatrix:
Expand All @@ -1464,7 +1466,7 @@ int64 GetMaxMemoryUse(const NnetComputation &computation) {
case kCompressMatrix:
cur_memory_use += this_compressed_num_bytes - this_num_bytes;
break;
case kUncompressMatrix:
case kDecompressMatrix:
cur_memory_use += this_num_bytes - this_compressed_num_bytes;
break;
default:
Expand Down
2 changes: 1 addition & 1 deletion src/nnet3/nnet-analyze.h
Expand Up @@ -423,7 +423,7 @@ class ComputationChecker {
void CheckComputationRewrite() const;
// Check matrix accesses make sense.
void CheckComputationMatrixAccesses() const;
// Some checks related to the kCompressMatrix and kUncompressMatrix commands.
// Some checks related to the kCompressMatrix and kDecompressMatrix commands.
void CheckComputationCompression() const;
// Check debug_info has the correct size, if used.
void CheckComputationDebugInfo() const;
Expand Down
26 changes: 19 additions & 7 deletions src/nnet3/nnet-computation.cc
Expand Up @@ -284,8 +284,8 @@ void NnetComputation::Command::Read(std::istream &is, bool binary) {
command_type = kAddRowRanges;
} else if (command_type_str == "kCompressMatrix") {
command_type = kCompressMatrix;
} else if (command_type_str == "kUncompressMatrix") {
command_type = kUncompressMatrix;
} else if (command_type_str == "kDecompressMatrix") {
command_type = kDecompressMatrix;
} else if (command_type_str == "kAcceptInput") {
command_type = kAcceptInput;
} else if (command_type_str == "kProvideOutput") {
Expand Down Expand Up @@ -382,8 +382,8 @@ void NnetComputation::Command::Write(std::ostream &os, bool binary) const {
case kCompressMatrix:
os << "kCompressMatrix\n";
break;
case kUncompressMatrix:
os << "kUncompressMatrix\n";
case kDecompressMatrix:
os << "kDecompressMatrix\n";
break;
case kAcceptInput:
os << "kAcceptInput\n";
Expand Down Expand Up @@ -510,13 +510,17 @@ static void GetIndexesMultiStrings(


// writes to "os" the statement for this command.
static void PrintCommand(std::ostream &os,
static void PrintCommand(std::ostream &os_out,
const Nnet &nnet,
const NnetComputation &computation,
int32 command_index,
const std::vector<std::string> &submatrix_strings,
const std::vector<std::string> &indexes_strings,
const std::vector<std::string> &indexes_multi_strings) {
// If the string is longer than 'max_string_length' characters, it will
// be summarized with '...' in the middle.
size_t max_string_length = 200;
std::ostringstream os;
KALDI_ASSERT(command_index < computation.commands.size());
os << "c" << command_index << ": ";
const NnetComputation::Command &c = computation.commands[command_index];
Expand Down Expand Up @@ -637,8 +641,8 @@ static void PrintCommand(std::ostream &os,
<< truncate << ")\n";
break;
}
case kUncompressMatrix:
os << "UncompressMatrix(" << submatrix_strings[c.arg1] << ")\n";
case kDecompressMatrix:
os << "DecompressMatrix(" << submatrix_strings[c.arg1] << ")\n";
break;
case kAcceptInput:
os << submatrix_strings[c.arg1] << " = user input [for node: '"
Expand Down Expand Up @@ -666,6 +670,14 @@ static void PrintCommand(std::ostream &os,
default:
KALDI_ERR << "Un-handled command type.";
}
std::string str = os.str();
if (str.size() <= max_string_length) {
os_out << str;
} else {
size_t len = str.size();
os_out << str.substr(0, max_string_length / 2) << " ... "
<< str.substr(len - max_string_length / 2);
}
}


Expand Down
4 changes: 2 additions & 2 deletions src/nnet3/nnet-computation.h
Expand Up @@ -241,7 +241,7 @@ struct ComputationRequest {
class CuCompressedMatrix; it should be false (0) if you know that
the input is limited to the allowed range, and true (1) if the
input may exceed that range (see docs for CuCompresedMatrix).
- kUncompressMatrix: Uncompresses the matrix which is referred to
- kDecompressMatrix: Decompresses the matrix which is referred to
by submatrix-index arg1 (it should previously have been compressed).
- kAcceptInput: accepts a matrix of input from the user, which may be either
features, or derivatives w.r.t. the output. arg1 is the submatrix index of
Expand Down Expand Up @@ -274,7 +274,7 @@ enum CommandType {
kPropagate, kBackprop, kBackpropNoModelUpdate,
kMatrixCopy, kMatrixAdd, kCopyRows, kAddRows,
kCopyRowsMulti, kCopyToRowsMulti, kAddRowsMulti, kAddToRowsMulti,
kAddRowRanges, kCompressMatrix, kUncompressMatrix,
kAddRowRanges, kCompressMatrix, kDecompressMatrix,
kAcceptInput, kProvideOutput,
kNoOperation, kNoOperationPermanent, kNoOperationMarker, kNoOperationLabel,
kGotoLabel };
Expand Down
2 changes: 1 addition & 1 deletion src/nnet3/nnet-compute.cc
Expand Up @@ -401,7 +401,7 @@ void NnetComputer::ExecuteCommand() {
}
#endif
}
case kUncompressMatrix: {
case kDecompressMatrix: {
#if HAVE_CUDA == 1
if (CuDevice::Instantiate().Enabled()) {
int32 m = computation_.submatrices[c.arg1].matrix_index;
Expand Down
2 changes: 1 addition & 1 deletion src/nnet3/nnet-compute.h
Expand Up @@ -164,7 +164,7 @@ class NnetComputer {
// NULL).
std::vector<void*> memos_;

// This is only used when commands kCompressMatrix and kUncompressMatrix are
// This is only used when commands kCompressMatrix and kDecompressMatrix are
// invoked. It will be (the first time we compress a matrix) resized to be
// the same size as 'matrices_' (i.e., indexed by matrix index). When we
// compress a matrix m we set compressed_matrices_[m] to a non-NULL value and
Expand Down
9 changes: 2 additions & 7 deletions src/nnet3/nnet-optimize-utils.cc
Expand Up @@ -3208,7 +3208,7 @@ void ComputationExpander::ComputeCommands() {
case kAddRowRanges:
ExpandRowRangesCommand(c, &c_out);
break;
case kCompressMatrix: case kUncompressMatrix:
case kCompressMatrix: case kDecompressMatrix:
case kAcceptInput: case kProvideOutput: case kNoOperation:
case kNoOperationPermanent: case kNoOperationMarker:
case kNoOperationLabel: case kGotoLabel:
Expand Down Expand Up @@ -4469,7 +4469,7 @@ void MemoryCompressionOptimizer::ModifyComputation() {
pairs_to_insert.push_back(p1);
std::pair<int32, NnetComputation::Command> p2(
info.uncompression_command_index,
NnetComputation::Command(1.0, kUncompressMatrix, s));
NnetComputation::Command(1.0, kDecompressMatrix, s));
pairs_to_insert.push_back(p2);
}
InsertCommands(&pairs_to_insert,
Expand Down Expand Up @@ -4502,11 +4502,6 @@ void MemoryCompressionOptimizer::ProcessMatrix(int32 m) {
std::vector<Access>::const_iterator iter = std::lower_bound(accesses.begin(),
accesses.end(),
middle_access);

if (m == 84) {
KALDI_LOG << "m == 84"; //TEMP
}

// At this point, 'iter' points to the first access in 'accesses'
// whose command index is >= 'middle_command_' (which separates the forward
// and backward passes), or accesses.end() if this matrix was not
Expand Down
2 changes: 1 addition & 1 deletion src/nnet3/nnet-optimize-utils.h
Expand Up @@ -552,7 +552,7 @@ void InsertCommands(
NnetComputation *computation);

/// Performs optimization to reduce memory usage where possible,
/// making use of the kCompressMatrix and kUncompressMatrix commands.
/// making use of the kCompressMatrix and kDecompressMatrix commands.
/// Should only be done after most other optimizations, because some
/// optimizations (such as variable-merging) would not work correctly
/// after doing this optimization. This does nothing for looped
Expand Down
10 changes: 2 additions & 8 deletions src/nnet3/nnet-optimize.cc
Expand Up @@ -478,7 +478,7 @@ void Optimize(const NnetOptimizeOptions &config,
const Nnet &nnet,
int32 max_output_time_in_request,
NnetComputation *computation) {
if (GetVerboseLevel() >= 1) { // TEMP, should be 3
if (GetVerboseLevel() >= 3) {
CheckComputation(nnet, *computation, true);
KALDI_LOG << "Before optimization, max memory use (bytes) = "
<< GetMaxMemoryUse(*computation);
Expand Down Expand Up @@ -594,15 +594,9 @@ void Optimize(const NnetOptimizeOptions &config,
computation);
if (GetVerboseLevel() >= 3)
CheckComputation(nnet, *computation, false);

{ // TEMP
std::ostringstream os;
computation->Print(os, nnet);
KALDI_LOG << "Compuation after adding memory compression is: " << os.str();
}
}

if (GetVerboseLevel() >= 1) { // TEMP, should be 3
if (GetVerboseLevel() >= 3) {
CheckComputation(nnet, *computation, false);
KALDI_LOG << "After optimization, max memory use (bytes) = "
<< GetMaxMemoryUse(*computation);
Expand Down

0 comments on commit 46cdd54

Please sign in to comment.