<commit_before>/* Copyright 2016 <NAME>, NVIDIA Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "scanner/evaluators/video/decoder_evaluator.h"
#include "scanner/evaluators/serialize.h"
#include "scanner/metadata.pb.h"

#include "scanner/util/memory.h"

namespace scanner {

DecoderEvaluator::DecoderEvaluator(const EvaluatorConfig& config,
                                   DeviceType device_type,
                                   VideoDecoderType decoder_type,
                                   i32 extra_outputs,
                                   i32 pu_count)
    : device_type_(device_type),
      device_id_(config.device_ids[0]),
      decoder_type_(decoder_type),
      needs_warmup_(false),
      discontinuity_(false),
      extra_outputs_(extra_outputs) {
  decoder_.reset(VideoDecoder::make_from_config(device_type_, device_id_,
                                                decoder_type_, device_type_,
                                                pu_count));
  assert(decoder_.get());
}

void DecoderEvaluator::configure(const InputFormat& metadata) {
  metadata_ = metadata;
  frame_size_ = metadata_.width() * metadata_.height() * 3;
  decoder_->configure(metadata);
}

void DecoderEvaluator::reset() {
  needs_warmup_ = true;
  discontinuity_ = true;
}

void DecoderEvaluator::evaluate(const BatchedColumns& input_columns,
                                BatchedColumns& output_columns) {
  assert(input_columns.size() == 2 + extra_outputs_);

  auto start = now();

  i64 total_frames_decoded = 0;
  i64 total_frames_used = 0;
  size_t num_inputs = input_columns.empty() ? 0 : input_columns[0].rows.size();
  for (size_t i = 0; i < num_inputs; ++i) {
    u8* decode_args_buffer = input_columns[1].rows[i].buffer;
    size_t decode_args_buffer_size = input_columns[1].rows[i].size;

    const u8* in_encoded_buffer = input_columns[0].rows[i].buffer;
    size_t in_encoded_buffer_size = input_columns[0].rows[i].size;

    DecodeArgs args;
    const u8* encoded_buffer;
    size_t encoded_buffer_size = in_encoded_buffer_size;
    if (device_type_ == DeviceType::GPU) {
#ifdef HAVE_CUDA
      u8* buffer = new u8[decode_args_buffer_size];
      memcpy_buffer(buffer, DeviceType::CPU, 0, decode_args_buffer,
                    DeviceType::GPU, device_id_, decode_args_buffer_size);
      args = deserialize_decode_args(buffer, decode_args_buffer_size);
      delete[] buffer;

      buffer = new u8[encoded_buffer_size];
      memcpy_buffer(buffer, DeviceType::CPU, 0, in_encoded_buffer,
                    DeviceType::GPU, device_id_, encoded_buffer_size);
      encoded_buffer = buffer;
#else

#endif
    } else {
      args =
          deserialize_decode_args(decode_args_buffer, decode_args_buffer_size);
      encoded_buffer = in_encoded_buffer;
    }

    std::vector<i32> valid_frames;
    if (args.sampling() == DecodeArgs::All) {
      const DecodeArgs::StridedInterval& interval = args.interval();
      i32 s = interval.start();
      if (!needs_warmup_) {
        s += std::min(
            args.warmup_count(),
            static_cast<i32>(args.rows_from_start() + total_frames_used));
      }
      for (; s < interval.end(); ++s) {
        valid_frames.push_back(s);
      }
    } else if (args.sampling() == DecodeArgs::Strided) {
      const DecodeArgs::StridedInterval& interval = args.interval();
      i32 s = interval.start();
      i32 e = interval.end();
      i32 stride = args.stride();
      if (!needs_warmup_) {
        s += std::min(
            args.warmup_count() * stride,
            static_cast<i32>(args.rows_from_start() + total_frames_used));
      }
      for (; s < e; s += stride) {
        valid_frames.push_back(s);
      }
    } else if (args.sampling() == DecodeArgs::Gather) {
      i32 s = 0;
      if (!needs_warmup_) {
        s += std::min(
            args.warmup_count(),
            static_cast<i32>(args.rows_from_start() + total_frames_used));
      }
      for (; s < args.gather_points_size(); ++s) {
        valid_frames.push_back(args.gather_points(s));<commit_msg>Fix strided gather not returning correct frames<commit_after>/* Copyright 2016 <NAME>, NVIDIA Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "scanner/evaluators/video/decoder_evaluator.h"
#include "scanner/evaluators/serialize.h"
#include "scanner/metadata.pb.h"

#include "scanner/util/memory.h"

namespace scanner {

DecoderEvaluator::DecoderEvaluator(const EvaluatorConfig& config,
                                   DeviceType device_type,
                                   VideoDecoderType decoder_type,
                                   i32 extra_outputs,
                                   i32 pu_count)
    : device_type_(device_type),
      device_id_(config.device_ids[0]),
      decoder_type_(decoder_type),
      needs_warmup_(false),
      discontinuity_(false),
      extra_outputs_(extra_outputs) {
  decoder_.reset(VideoDecoder::make_from_config(device_type_, device_id_,
                                                decoder_type_, device_type_,
                                                pu_count));
  assert(decoder_.get());
}

void DecoderEvaluator::configure(const InputFormat& metadata) {
  metadata_ = metadata;
  frame_size_ = metadata_.width() * metadata_.height() * 3;
  decoder_->configure(metadata);
}

void DecoderEvaluator::reset() {
  needs_warmup_ = true;
  discontinuity_ = true;
}

void DecoderEvaluator::evaluate(const BatchedColumns& input_columns,
                                BatchedColumns& output_columns) {
  assert(input_columns.size() == 2 + extra_outputs_);

  auto start = now();

  i64 total_frames_decoded = 0;
  i64 total_frames_used = 0;
  size_t num_inputs = input_columns.empty() ? 0 : input_columns[0].rows.size();
  for (size_t i = 0; i < num_inputs; ++i) {
    u8* decode_args_buffer = input_columns[1].rows[i].buffer;
    size_t decode_args_buffer_size = input_columns[1].rows[i].size;

    const u8* in_encoded_buffer = input_columns[0].rows[i].buffer;
    size_t in_encoded_buffer_size = input_columns[0].rows[i].size;

    DecodeArgs args;
    const u8* encoded_buffer;
    size_t encoded_buffer_size = in_encoded_buffer_size;
    if (device_type_ == DeviceType::GPU) {
#ifdef HAVE_CUDA
      u8* buffer = new u8[decode_args_buffer_size];
      memcpy_buffer(buffer, DeviceType::CPU, 0, decode_args_buffer,
                    DeviceType::GPU, device_id_, decode_args_buffer_size);
      args = deserialize_decode_args(buffer, decode_args_buffer_size);
      delete[] buffer;

      buffer = new u8[encoded_buffer_size];
      memcpy_buffer(buffer, DeviceType::CPU, 0, in_encoded_buffer,
                    DeviceType::GPU, device_id_, encoded_buffer_size);
      encoded_buffer = buffer;
#else

#endif
    } else {
      args =
          deserialize_decode_args(decode_args_buffer, decode_args_buffer_size);
      encoded_buffer = in_encoded_buffer;
    }

    std::vector<i32> valid_frames;
    if (args.sampling() == DecodeArgs::All) {
      const DecodeArgs::StridedInterval& interval = args.interval();
      i32 s = interval.start();
      if (!needs_warmup_) {
        s += std::min(
            args.warmup_count(),
            static_cast<i32>(args.rows_from_start() + total_frames_used));
      }
      for (; s < interval.end(); ++s) {
        valid_frames.push_back(s);
      }
    } else if (args.sampling() == DecodeArgs::Strided) {
      const DecodeArgs::StridedInterval& interval = args.interval();
      i32 s = interval.start();
      i32 e = interval.end();
      i32 stride = args.stride();
      if (!needs_warmup_) {
        s += std::min(
            args.warmup_count() * stride,
            static_cast<i32>(args.rows_from_start() + total_frames_used));
      }
      for (; s < e; s += stride) {
        valid_frames.push_back(s);
      }
      discontinuity_ = true;
    } else if (args.sampling() == DecodeArgs::Gather) {
      i32 s = 0;
      if (!needs_warmup_) {
        s += std::min(
            args.warmup_count(),
            static_cast<i32>(args.rows_from_start() + total_frames_used));
      }
      for (; s < args.gather_points_size(); ++s) {
        valid_frames.push_back(args.gather_points(s));
{'fraction_non_alphanumeric': 0.10895700205783602, 'fraction_numerical': 0.011913787501353839, 'mean_word_length': 2.8, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3129816', 'n_tokens_mistral': 2877, 'n_tokens_neox': 2750, 'n_words': 607}