Repository: tesseract-ocr/tesseract
File: src/lstm/weightmatrix.cpp (C++)

Code before:
    /* The scales stored in memory have an extra factor applied to them
     * to allow faster operation. We have to remove that factor here
     * before writing to disc, and put it back afterwards. */
    scales_.scale((double)INT8_MAX);
    if (!scales_.Serialize(fp)) return false;
    scales_.scale((double)1.0/INT8_MAX);
  } else {
    if (!wf_.Serialize(fp)) return false;
    if (training && !updates_.Serialize(fp)) return false;
    if (training && use_adam_ && !dw_sq_sum_.Serialize(fp)) return false;
  }
  return true;
}

// Reads from the given file. Returns false in case of error.

bool WeightMatrix::DeSerialize(bool training, TFile* fp) {
  uint8_t mode;
  if (!fp->DeSerialize(&mode)) return false;
  int_mode_ = (mode & kInt8Flag) != 0;
  use_adam_ = (mode & kAdamFlag) != 0;
  if ((mode & kDoubleFlag) == 0) return DeSerializeOld(training, fp);
  if (int_mode_) {
    if (!wi_.DeSerialize(fp)) return false;
    if (!scales_.DeSerialize(fp)) return false;
    scales_.scale((double)1.0/INT8_MAX);
    if (IntSimdMatrix::intSimdMatrix) {
      IntSimdMatrix::intSimdMatrix->Init(wi_, shaped_w_, scales_);
    }
  } else {
    if (!wf_.DeSerialize(fp)) return false;
    if (training) {
      InitBackward();
      if (!updates_.DeSerialize(fp)) return false;
      if (use_adam_ && !dw_sq_sum_.DeSerialize(fp)) return false;
    }
  }
  return true;
}

// As DeSerialize, but reads an old (float) format WeightMatrix for
// backward compatibility.
bool WeightMatrix::DeSerializeOld(bool training, TFile* fp) {
  GENERIC_2D_ARRAY<float> float_array;
  if (int_mode_) {
    if (!wi_.DeSerialize(fp)) return false;
    GenericVector<float> old_scales;
    if (!old_scales.DeSerialize(fp)) return false;
    scales_.resize_no_init(old_scales.size());
    for (int i = 0; i < old_scales.size(); ++i) scales_[i] = old_scales[i];
  } else {

Review comment (suggestion):
```suggestion
    scales_.scale(1.0 / INT8_MAX);
```

Code after:
    /* The scales stored in memory have an extra factor applied to them
     * to allow faster operation. We have to remove that factor here
     * before writing to disc, and put it back afterwards. */
    scales_.scale(INT8_MAX);
    if (!scales_.Serialize(fp)) return false;
    scales_.scale(1.0/INT8_MAX);
  } else {
    if (!wf_.Serialize(fp)) return false;
    if (training && !updates_.Serialize(fp)) return false;
    if (training && use_adam_ && !dw_sq_sum_.Serialize(fp)) return false;
  }
  return true;
}

// Reads from the given file. Returns false in case of error.

bool WeightMatrix::DeSerialize(bool training, TFile* fp) {
  uint8_t mode;
  if (!fp->DeSerialize(&mode)) return false;
  int_mode_ = (mode & kInt8Flag) != 0;
  use_adam_ = (mode & kAdamFlag) != 0;
  if ((mode & kDoubleFlag) == 0) return DeSerializeOld(training, fp);
  if (int_mode_) {
    if (!wi_.DeSerialize(fp)) return false;
    if (!scales_.DeSerialize(fp)) return false;
    scales_.scale(1.0/INT8_MAX);
    if (IntSimdMatrix::intSimdMatrix) {
      IntSimdMatrix::intSimdMatrix->Init(wi_, shaped_w_, scales_);
    }
  } else {
    if (!wf_.DeSerialize(fp)) return false;
    if (training) {
      InitBackward();
      if (!updates_.DeSerialize(fp)) return false;
      if (use_adam_ && !dw_sq_sum_.DeSerialize(fp)) return false;
    }
  }
  return true;
}

// As DeSerialize, but reads an old (float) format WeightMatrix for
// backward compatibility.
bool WeightMatrix::DeSerializeOld(bool training, TFile* fp) {
  GENERIC_2D_ARRAY<float> float_array;
  if (int_mode_) {
    if (!wi_.DeSerialize(fp)) return false;
    GenericVector<float> old_scales;
    if (!old_scales.DeSerialize(fp)) return false;
    scales_.resize_no_init(old_scales.size());
    for (int i = 0; i < old_scales.size(); ++i) scales_[i] = old_scales[i];
  } else {