void SemanticDictionary::CreatePredicateRoleDictionaries(SemanticReader *reader) {
    LOG(INFO) << "Creating predicate and role dictionaries...";

    // Initialize lemma predicates.
    int num_lemmas = token_dictionary_->GetNumLemmas();
    lemma_predicates_.resize(num_lemmas);

    vector<int> role_freqs;
    vector<int> predicate_freqs;

    string special_symbols[NUM_SPECIAL_PREDICATES];
    special_symbols[PREDICATE_UNKNOWN] = kPredicateUnknown;
    for (int i = 0; i < NUM_SPECIAL_PREDICATES; ++i) {
        predicate_alphabet_.Insert(special_symbols[i]);

        // Counts of special symbols are set to -1:
        predicate_freqs.push_back(-1);
    }

    // Go through the corpus and build the predicate/roles dictionaries,
    // counting the frequencies.
    reader->Open(pipe_->GetOptions()->GetTrainingFilePath());
    SemanticInstance *instance =
        static_cast<SemanticInstance*>(reader->GetNext());
    int instance_length = instance->size();
    while (instance != NULL) {
        for (int k = 0; k < instance->GetNumPredicates(); ++k) {
            int i = instance->GetPredicateIndex(k);
            const std::string lemma = instance->GetLemma(i);
            const std::string predicate_name = instance->GetPredicateName(k);

            // Get the lemma integer representation.
            int lemma_id = token_dictionary_->GetLemmaId(lemma);

            // If the lemma does not exist, the predicate will not be added.
            SemanticPredicate *predicate = NULL;
            if (lemma_id >= 0) {
                // Add predicate name to alphabet.
                int predicate_id =
                    predicate_alphabet_.Insert(predicate_name);
                if (predicate_id >= predicate_freqs.size()) {
                    CHECK_EQ(predicate_id, predicate_freqs.size());
                    predicate_freqs.push_back(0);
                }
                ++predicate_freqs[predicate_id];

                // Add predicate to the list of lemma predicates.
                std::vector<SemanticPredicate*> *predicates =
                    &lemma_predicates_[lemma_id];
                for (int j = 0; j < predicates->size(); ++j) {
                    if ((*predicates)[j]->id() == predicate_id) {
                        predicate = (*predicates)[j];
                    }
                }
                if (!predicate) {
                    predicate = new SemanticPredicate(predicate_id);
                    predicates->push_back(predicate);
                }
            }

            // Add semantic roles to alphabet.
            for (int l = 0; l < instance->GetNumArgumentsPredicate(k); ++l) {
                int role_id = role_alphabet_.Insert(instance->GetArgumentRole(k, l));
                if (role_id >= role_freqs.size()) {
                    CHECK_EQ(role_id, role_freqs.size());
                    role_freqs.push_back(0);
                }
                ++role_freqs[role_id];
                // Add this role to the predicate.
                if (predicate && !predicate->HasRole(role_id)) {
                    predicate->InsertRole(role_id);
                }
            }
        }
        delete instance;
        instance = static_cast<SemanticInstance*>(reader->GetNext());
    }
    reader->Close();
    role_alphabet_.StopGrowth();

    // Take care of the special "unknown" predicate.
    bool allow_unseen_predicates =
        static_cast<SemanticPipe*>(pipe_)->GetSemanticOptions()->
        allow_unseen_predicates();
    bool use_predicate_senses =
        static_cast<SemanticPipe*>(pipe_)->GetSemanticOptions()->
        use_predicate_senses();
    if (allow_unseen_predicates || !use_predicate_senses) {
        // 1) Add the predicate as the singleton list of lemma predicates for the
        // "unknown" lemma.
        std::vector<SemanticPredicate*> *predicates =
            &lemma_predicates_[TOKEN_UNKNOWN];
        CHECK_EQ(predicates->size(), 0);
        SemanticPredicate *predicate = new SemanticPredicate(PREDICATE_UNKNOWN);
        predicates->push_back(predicate);

        // 2) Add all possible roles to the special "unknown" predicate.
        for (int role_id = 0; role_id < role_alphabet_.size(); ++role_id) {
            if (!predicate->HasRole(role_id)) predicate->InsertRole(role_id);
        }
    }

    predicate_alphabet_.StopGrowth();

    CHECK_LT(predicate_alphabet_.size(), kMaxPredicateAlphabetSize);
    CHECK_LT(role_alphabet_.size(), kMaxRoleAlphabetSize);


    // Prepare alphabets for dependency paths (relations and POS).
    vector<int> relation_path_freqs;
    Alphabet relation_path_alphabet;
    vector<int> pos_path_freqs;
    Alphabet pos_path_alphabet;

    string special_path_symbols[NUM_SPECIAL_PATHS];
    special_path_symbols[PATH_UNKNOWN] = kPathUnknown;
    for (int i = 0; i < NUM_SPECIAL_PATHS; ++i) {
        relation_path_alphabet.Insert(special_path_symbols[i]);
        pos_path_alphabet.Insert(special_path_symbols[i]);

        // Counts of special symbols are set to -1:
        relation_path_freqs.push_back(-1);
        pos_path_freqs.push_back(-1);
    }

    // Go through the corpus and build the existing labels for:
    // - each head-modifier POS pair,
    // - each syntactic path (if available).
    // Keep also the maximum left/right arc lengths for each pair of POS tags.
    existing_roles_.clear();
    existing_roles_.resize(token_dictionary_->GetNumPosTags(),
                           vector<vector<int> >(
                               token_dictionary_->GetNumPosTags()));

    vector<vector<int> > existing_roles_with_relation_path;
    vector<int> role_pair_freqs(GetNumRoleBigramLabels(), 0);
    // Initialize every label as deterministic.
    deterministic_roles_.assign(GetNumRoles(), true);

    maximum_left_distances_.clear();
    maximum_left_distances_.resize(token_dictionary_->GetNumPosTags(),
                                   vector<int>(
                                       token_dictionary_->GetNumPosTags(), 0));

    maximum_right_distances_.clear();
    maximum_right_distances_.resize(token_dictionary_->GetNumPosTags(),
                                    vector<int>(
                                        token_dictionary_->GetNumPosTags(), 0));

    reader->Open(pipe_->GetOptions()->GetTrainingFilePath());
    instance = static_cast<SemanticInstance*>(reader->GetNext());
    while (instance != NULL) {
        int instance_length = instance->size();
        for (int k = 0; k < instance->GetNumPredicates(); ++k) {
            int p = instance->GetPredicateIndex(k);
            const string &predicate_pos = instance->GetPosTag(p);
            int predicate_pos_id = token_dictionary_->GetPosTagId(predicate_pos);
            if (predicate_pos_id < 0) predicate_pos_id = TOKEN_UNKNOWN;

            // Add semantic roles to alphabet.
            for (int l = 0; l < instance->GetNumArgumentsPredicate(k); ++l) {
                int a = instance->GetArgumentIndex(k, l);
                const string &argument_pos = instance->GetPosTag(a);
                int argument_pos_id = token_dictionary_->GetPosTagId(argument_pos);
                if (argument_pos_id < 0) argument_pos_id = TOKEN_UNKNOWN;
                int role_id = role_alphabet_.Lookup(instance->GetArgumentRole(k, l));
                CHECK_GE(role_id, 0);

                // Look for possible role pairs.
                for (int m = l+1; m < instance->GetNumArgumentsPredicate(k); ++m) {
                    int other_role_id =
                        role_alphabet_.Lookup(instance->GetArgumentRole(k, m));
                    CHECK_GE(other_role_id, 0);
                    int bigram_label = GetRoleBigramLabel(role_id, other_role_id);
                    CHECK_GE(bigram_label, 0);
                    CHECK_LT(bigram_label, GetNumRoleBigramLabels());
                    ++role_pair_freqs[bigram_label];
                    if (role_id == other_role_id) {
                        // Role label is not deterministic.
                        deterministic_roles_[role_id] = false;
                    }
                }

                // Insert new role in the set of existing labels, if it is not there
                // already. NOTE: this is inefficient, maybe we should be using a
                // different data structure.
                vector<int> &roles = existing_roles_[predicate_pos_id][argument_pos_id];
                int j;
                for (j = 0; j < roles.size(); ++j) {
                    if (roles[j] == role_id) break;
                }
                if (j == roles.size()) roles.push_back(role_id);

                // Update the maximum distances if necessary.
                if (p < a) {
                    // Right attachment.
                    if (a - p >
                            maximum_right_distances_[predicate_pos_id][argument_pos_id]) {
                        maximum_right_distances_[predicate_pos_id][argument_pos_id] = a - p;
                    }
                } else {
                    // Left attachment (or self-loop). TODO(atm): treat self-loops differently?
                    if (p - a >
                            maximum_left_distances_[predicate_pos_id][argument_pos_id]) {
                        maximum_left_distances_[predicate_pos_id][argument_pos_id] = p - a;
                    }
                }

                // Compute the syntactic path between the predicate and the argument and
                // add it to the dictionary.
                string relation_path;
                string pos_path;
                ComputeDependencyPath(instance, p, a, &relation_path, &pos_path);
                int relation_path_id = relation_path_alphabet.Insert(relation_path);
                if (relation_path_id >= relation_path_freqs.size()) {
                    CHECK_EQ(relation_path_id, relation_path_freqs.size());
                    relation_path_freqs.push_back(0);
                }
                ++relation_path_freqs[relation_path_id];
                int pos_path_id = pos_path_alphabet.Insert(pos_path);
                if (pos_path_id >= pos_path_freqs.size()) {
                    CHECK_EQ(pos_path_id, pos_path_freqs.size());
                    pos_path_freqs.push_back(0);
                }
                ++pos_path_freqs[pos_path_id];

                // Insert new role in the set of existing labels with this relation
                // path, if it is not there already. NOTE: this is inefficient, maybe we
                // should be using a different data structure.
                if (relation_path_id >= existing_roles_with_relation_path.size()) {
                    existing_roles_with_relation_path.resize(relation_path_id + 1);
                }
                vector<int> &path_roles =
                    existing_roles_with_relation_path[relation_path_id];
                for (j = 0; j < path_roles.size(); ++j) {
                    if (path_roles[j] == role_id) break;
                }
                if (j == path_roles.size()) path_roles.push_back(role_id);
            }
        }
        delete instance;
        instance = static_cast<SemanticInstance*>(reader->GetNext());
    }
    reader->Close();

    // Now adjust the cutoffs if necessary.
    int relation_path_cutoff = FLAGS_relation_path_cutoff;
    while (true) {
        relation_path_alphabet_.clear();
        existing_roles_with_relation_path_.clear();
        for (int i = 0; i < NUM_SPECIAL_PATHS; ++i) {
            int relation_path_id =
                relation_path_alphabet_.Insert(special_path_symbols[i]);
            vector<int> &roles = existing_roles_with_relation_path[i];
            CHECK_EQ(roles.size(), 0);
            existing_roles_with_relation_path_.push_back(roles);
            //existing_roles_with_relation_path_.push_back(vector<int>(0));
        }
        for (Alphabet::iterator iter = relation_path_alphabet.begin();
                iter != relation_path_alphabet.end();
                ++iter) {
            if (relation_path_freqs[iter->second] > relation_path_cutoff) {
                int relation_path_id = relation_path_alphabet_.Insert(iter->first);
                vector<int> &roles = existing_roles_with_relation_path[iter->second];
                existing_roles_with_relation_path_.push_back(roles);
            }
        }
        CHECK_EQ(relation_path_alphabet_.size(),
                 existing_roles_with_relation_path_.size());
        if (relation_path_alphabet_.size() < kMaxRelationPathAlphabetSize) break;
        ++relation_path_cutoff;
        CHECK(false); // For now, disallowed: this would mess up the relation path filter.
        LOG(INFO) << "Incrementing relation path cutoff to "
                  << relation_path_cutoff << "...";
    }

    int pos_path_cutoff = FLAGS_pos_path_cutoff;
    while (true) {
        pos_path_alphabet_.clear();
        for (int i = 0; i < NUM_SPECIAL_PATHS; ++i) {
            pos_path_alphabet_.Insert(special_path_symbols[i]);
        }
        for (Alphabet::iterator iter = pos_path_alphabet.begin();
                iter != pos_path_alphabet.end();
                ++iter) {
            if (pos_path_freqs[iter->second] > pos_path_cutoff) {
                pos_path_alphabet_.Insert(iter->first);
            }
        }
        if (pos_path_alphabet_.size() < kMaxPosPathAlphabetSize) break;
        ++pos_path_cutoff;
        LOG(INFO) << "Incrementing pos path cutoff to "
                  << pos_path_cutoff << "...";
    }

    relation_path_alphabet_.StopGrowth();
    pos_path_alphabet_.StopGrowth();

    CHECK_LT(relation_path_alphabet_.size(), kMaxRelationPathAlphabetSize);
    CHECK_LT(pos_path_alphabet_.size(), kMaxPosPathAlphabetSize);

    // Compute the set of most frequent role pairs.
    vector<pair<int, int> > freqs_pairs;
    for (int k = 0; k < role_pair_freqs.size(); ++k) {
        freqs_pairs.push_back(pair<int,int>(-role_pair_freqs[k], k));
    }
    sort(freqs_pairs.begin(), freqs_pairs.end());
    frequent_role_pairs_.clear();
    for (int k = 0;
            k < FLAGS_num_frequent_role_pairs && k < freqs_pairs.size();
            ++k) {
        frequent_role_pairs_.insert(freqs_pairs[k].second);
    }

    // Display information about frequent role pairs.
    for (Alphabet::iterator it = role_alphabet_.begin();
            it != role_alphabet_.end(); ++it) {
        string first_role = it->first;
        int first_role_id = it->second;
        for (Alphabet::iterator it2 = role_alphabet_.begin();
                it2 != role_alphabet_.end(); ++it2) {
            string second_role = it2->first;
            int second_role_id = it2->second;
            if (IsFrequentRolePair(first_role_id, second_role_id)) {
                LOG(INFO) << "Frequent role pair: "
                          << first_role << " " << second_role;
            }
        }
    }

    // Display information about deterministic roles.
    int num_deterministic_roles = 0;
    for (Alphabet::iterator it = role_alphabet_.begin();
            it != role_alphabet_.end(); ++it) {
        string role = it->first;
        int role_id = it->second;
        if (IsRoleDeterministic(role_id)) {
            LOG(INFO) << "Deterministic role: "
                      << role;
            ++num_deterministic_roles;
        }
    }
    LOG(INFO) << num_deterministic_roles << " out of "
              << GetNumRoles() << " roles are deterministic.";

#if 0
    // Go again through the corpus to build the existing labels for:
    // - each syntactic path (if available).
    reader->Open(pipe_->GetOptions()->GetTrainingFilePath());
    instance = static_cast<SemanticInstance*>(reader->GetNext());
    while (instance != NULL) {
        int instance_length = instance->size();
        for (int k = 0; k < instance->GetNumPredicates(); ++k) {
            int p = instance->GetPredicateIndex(k);
            const string &predicate_pos = instance->GetPosTag(p);
            int predicate_pos_id = token_dictionary_->GetPosTagId(predicate_pos);
            if (predicate_pos_id < 0) predicate_pos_id = TOKEN_UNKNOWN;

            // Add semantic roles to alphabet.
            for (int l = 0; l < instance->GetNumArgumentsPredicate(k); ++l) {
                int a = instance->GetArgumentIndex(k, l);
                const string &argument_pos = instance->GetPosTag(a);
                int argument_pos_id = token_dictionary_->GetPosTagId(argument_pos);
                if (argument_pos_id < 0) argument_pos_id = TOKEN_UNKNOWN;
                int role_id = role_alphabet_.Lookup(instance->GetArgumentRole(k, l));
                CHECK_GE(role_id, 0);

                // Compute the syntactic path between the predicate and the argument and
                // add it to the dictionary.
                string relation_path;
                ComputeDependencyPath(instance, p, a, &relation_path, &pos_path);
                int relation_path_id = relation_path_alphabet_.Get(relation_path);
                CHECK_GE(relation_path_id, 0);

                // Insert new role in the set of existing labels with this relation
                // path, if it is not there already. NOTE: this is inefficient, maybe we
                // should be using a different data structure.
                if (relation_path_id >= existing_roles_with_relation_path_.size()) {
                    existing_roles_with_relation_path_.resize(relation_path_id + 1);
                }
                vector<int> &path_roles = existing_roles_with_relation_path_[relation_path_id];
                for (j = 0; j < path_roles.size(); ++j) {
                    if (path_roles[j] == role_id) break;
                }
                if (j == path_roles.size()) path_roles.push_back(role_id);
            }
        }
        delete instance;
        instance = static_cast<SemanticInstance*>(reader->GetNext());
    }
    reader->Close();
#endif

    // Show corpus statistics.
    LOG(INFO) << "Number of predicates: " << predicate_alphabet_.size();
    LOG(INFO) << "Number of roles: " << role_alphabet_.size();
    LOG(INFO) << "Number of relation paths: " << relation_path_alphabet_.size();
    LOG(INFO) << "Number of POS paths: " << pos_path_alphabet_.size();
}
// Interprets ptr1, ptr2 as the ptr and len pair used for variable length data.
TargetValue ResultSet::makeVarlenTargetValue(const int8_t* ptr1,
                                             const int8_t compact_sz1,
                                             const int8_t* ptr2,
                                             const int8_t compact_sz2,
                                             const TargetInfo& target_info,
                                             const size_t target_logical_idx,
                                             const size_t entry_buff_idx) const {
  auto varlen_ptr = read_int_from_buff(ptr1, compact_sz1);
  if (none_encoded_strings_valid_) {
    if (varlen_ptr < 0) {
      CHECK_EQ(-1, varlen_ptr);
      return TargetValue(nullptr);
    }
    const auto storage_idx = getStorageIndex(entry_buff_idx);
    CHECK_LT(static_cast<size_t>(storage_idx.first), none_encoded_strings_.size());
    const auto none_encoded_strings = none_encoded_strings_[storage_idx.first];
    CHECK_LT(static_cast<size_t>(varlen_ptr), none_encoded_strings.size());
    return none_encoded_strings[varlen_ptr];
  }
  if (!lazy_fetch_info_.empty()) {
    CHECK_LT(target_logical_idx, lazy_fetch_info_.size());
    const auto& col_lazy_fetch = lazy_fetch_info_[target_logical_idx];
    if (col_lazy_fetch.is_lazily_fetched) {
      const auto storage_idx = getStorageIndex(entry_buff_idx);
      CHECK_LT(static_cast<size_t>(storage_idx.first), col_buffers_.size());
      auto& frag_col_buffers = getColumnFrag(storage_idx.first, target_logical_idx, varlen_ptr);
      bool is_end{false};
      if (target_info.sql_type.is_string()) {
        VarlenDatum vd;
        ChunkIter_get_nth(
            reinterpret_cast<ChunkIter*>(const_cast<int8_t*>(frag_col_buffers[col_lazy_fetch.local_col_id])),
            varlen_ptr,
            false,
            &vd,
            &is_end);
        CHECK(!is_end);
        if (vd.is_null) {
          return TargetValue(nullptr);
        }
        CHECK(vd.pointer);
        CHECK_GT(vd.length, 0);
        std::string fetched_str(reinterpret_cast<char*>(vd.pointer), vd.length);
        return fetched_str;
      } else {
        CHECK(target_info.sql_type.is_array());
        ArrayDatum ad;
        ChunkIter_get_nth(
            reinterpret_cast<ChunkIter*>(const_cast<int8_t*>(frag_col_buffers[col_lazy_fetch.local_col_id])),
            varlen_ptr,
            &ad,
            &is_end);
        CHECK(!is_end);
        if (ad.is_null) {
          std::vector<ScalarTargetValue> empty_array;
          return TargetValue(empty_array);
        }
        CHECK(ad.pointer);
        CHECK_GT(ad.length, 0);
        return build_array_target_value(target_info.sql_type, ad.pointer, ad.length, row_set_mem_owner_, executor_);
      }
    }
  }
  if (!varlen_ptr) {
    return TargetValue(nullptr);
  }
  auto length = read_int_from_buff(ptr2, compact_sz2);
  if (target_info.sql_type.is_array()) {
    const auto& elem_ti = target_info.sql_type.get_elem_type();
    length *= elem_ti.get_logical_size();
  }
  std::vector<int8_t> cpu_buffer;
  if (varlen_ptr && device_type_ == ExecutorDeviceType::GPU) {
    cpu_buffer.resize(length);
    auto& data_mgr = query_mem_desc_.executor_->catalog_->get_dataMgr();
    copy_from_gpu(&data_mgr, &cpu_buffer[0], static_cast<CUdeviceptr>(varlen_ptr), length, device_id_);
    varlen_ptr = reinterpret_cast<int64_t>(&cpu_buffer[0]);
  }
  if (target_info.sql_type.is_array()) {
    return build_array_target_value(
        target_info.sql_type, reinterpret_cast<const int8_t*>(varlen_ptr), length, row_set_mem_owner_, executor_);
  }
  return std::string(reinterpret_cast<char*>(varlen_ptr), length);
}
sp<MediaSource> ARTPSession::trackAt(size_t index) {
    CHECK_LT(index, mTracks.size());
    return mTracks.editItemAt(index).mPacketSource;
}
Exemple #4
0
void PoolingLayer<Dtype>::layerSetup(const vector<Blob<Dtype>*> &bottom, const vector<Blob<Dtype>*> &top){
	PoolingParameter pool_param = param.pooling_param();
	//	user need not set the kernel size if use global pooling
	if (pool_param.global_pooling()){
		CHECK(!(pool_param.has_kernel() ||
			pool_param.has_kernel_h() || pool_param.has_kernel_w()))
			<< "Can not specific kernel when using global pooling.";
	}
	else{
		if (pool_param.has_kernel() && (pool_param.has_kernel_w() && pool_param.has_kernel_w()))
			LOG(FATAL) << "Can not specific kernel or kernel_h/w both at the same time.";
		if (!pool_param.has_kernel() && !(pool_param.has_kernel_w() && pool_param.has_kernel_w()))
			LOG(FATAL) << "Must specific kernel or kernel_h/w both either.";
	}
	if (pool_param.has_pad() && (pool_param.has_kernel_w() && pool_param.has_kernel_w()))
		LOG(FATAL) << "Can not specific kernel or kernel_h/w both at the same time.";
	if (!pool_param.has_kernel() && !(pool_param.has_kernel_w() && pool_param.has_kernel_w()))
		LOG(FATAL) << "Must specific kernel or kernel_h/w both either.";
	global_pooling = pool_param.global_pooling();

	//	set kernel
	if (global_pooling){
		kernel_h = bottom[0]->height();
		kernel_w = bottom[0]->width();
	}
	else{
		if (!pool_param.has_kernel_h()) kernel_h = kernel_w = pool_param.kernel();
		else{
			kernel_h = pool_param.kernel_h();
			kernel_w = pool_param.kernel_w();
		}
	}
	CHECK_GT(kernel_h, 0) << "kernel_h must greater than zero";
	CHECK_GT(kernel_w, 0) << "kernel_w must greater than zero";
	//	set pad
	if (!pool_param.has_pad_h()) pad_h = pad_w = pool_param.pad();
	else{
		pad_h = pool_param.pad_h();
		pad_w = pool_param.pad_w();
	}
	//	set stride
	if (!pool_param.has_stride_h()) stride_h = stride_w = pool_param.stride();
	else{
		stride_h = pool_param.stride_h();
		stride_w = pool_param.stride_w();
	}
	CHECK_GT(stride_h, 0) << "stride_h must greater than zero";
	CHECK_GT(stride_w, 0) << "stride_w must greater than zero";
	//	check pad/stride
	if (global_pooling)
		CHECK(pad_h == 0 && pad_w == 0 && stride_h == 1 && stride_w == 1)
		<< "pad/stride must equal to zero/one when using global pooling.";
	//	check pad/kernel
	if (pad_h != 0 || pad_w != 0){
		CHECK(pool_param.method() == PoolingParameter_Method_MAX ||
			pool_param.method() == PoolingParameter_Method_AVG)
			<< "Padding only for MAX/AVG pooling.";
		CHECK_LT(pad_h, kernel_h) << "pad_h must less than kernel_h or do nothing.";
		CHECK_LT(pad_w, kernel_w) << "pad_w must less than kernel_w or do nothing.";
	}
}
sp<ABuffer> AMPEG4AudioAssembler::removeLATMFraming(const sp<ABuffer> &buffer) {
    CHECK(!mMuxConfigPresent);  // XXX to be implemented

    sp<ABuffer> out = new ABuffer(buffer->size());
    out->setRange(0, 0);

    size_t offset = 0;
    uint8_t *ptr = buffer->data();

    for (size_t i = 0; i <= mNumSubFrames; ++i) {
        // parse PayloadLengthInfo

        unsigned payloadLength = 0;

        switch (mFrameLengthType) {
            case 0:
            {
                unsigned muxSlotLengthBytes = 0;
                unsigned tmp;
                do {
                    CHECK_LT(offset, buffer->size());
                    tmp = ptr[offset++];
                    muxSlotLengthBytes += tmp;
                } while (tmp == 0xff);

                payloadLength = muxSlotLengthBytes;
                break;
            }

            case 2:
            {
                // reserved

                TRESPASS();
                break;
            }

            default:
            {
                CHECK_GE(mFixedFrameLength, 0);

                payloadLength = mFixedFrameLength;
                break;
            }
        }

        CHECK_LE(offset + payloadLength, buffer->size());

        memcpy(out->data() + out->size(), &ptr[offset], payloadLength);
        out->setRange(0, out->size() + payloadLength);

        offset += payloadLength;

        if (mOtherDataPresent) {
            // We want to stay byte-aligned.

            CHECK((mOtherDataLenBits % 8) == 0);
            CHECK_LE(offset + (mOtherDataLenBits / 8), buffer->size());
            offset += mOtherDataLenBits / 8;
        }
    }

    if (offset < buffer->size()) {
        LOGI("ignoring %d bytes of trailing data", buffer->size() - offset);
    }
    CHECK_LE(offset, buffer->size());

    return out;
}
void NuPlayer::RTSPSource::onMessageReceived(const sp<AMessage> &msg) {
    if (msg->what() == kWhatDisconnect) {
        sp<AReplyToken> replyID;
        CHECK(msg->senderAwaitsResponse(&replyID));

        mDisconnectReplyID = replyID;
        finishDisconnectIfPossible();
        return;
    } else if (msg->what() == kWhatPerformSeek) {
        int32_t generation;
        CHECK(msg->findInt32("generation", &generation));
        CHECK(msg->senderAwaitsResponse(&mSeekReplyID));

        if (generation != mSeekGeneration) {
            // obsolete.
            finishSeek(OK);
            return;
        }

        int64_t seekTimeUs;
        CHECK(msg->findInt64("timeUs", &seekTimeUs));

        performSeek(seekTimeUs);
        return;
    }

    CHECK_EQ(msg->what(), (int)kWhatNotify);

    int32_t what;
    CHECK(msg->findInt32("what", &what));

    switch (what) {
        case MyHandler::kWhatConnected:
        {
            onConnected();

            notifyVideoSizeChanged();

            uint32_t flags = 0;

            if (mHandler->isSeekable()) {
                flags = FLAG_CAN_PAUSE
                        | FLAG_CAN_SEEK
                        | FLAG_CAN_SEEK_BACKWARD
                        | FLAG_CAN_SEEK_FORWARD;
            }

            notifyFlagsChanged(flags);
            notifyPrepared();
            break;
        }

        case MyHandler::kWhatDisconnected:
        {
            onDisconnected(msg);
            break;
        }

        case MyHandler::kWhatSeekDone:
        {
            mState = CONNECTED;
            if (mSeekReplyID != NULL) {
                // Unblock seekTo here in case we attempted to seek in a live stream
                finishSeek(OK);
            }
            break;
        }

        case MyHandler::kWhatSeekPaused:
        {
            sp<AnotherPacketSource> source = getSource(true /* audio */);
            if (source != NULL) {
                source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
                        /* extra */ NULL,
                        /* discard */ true);
            }
            source = getSource(false /* video */);
            if (source != NULL) {
                source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
                        /* extra */ NULL,
                        /* discard */ true);
            };

            status_t err = OK;
            msg->findInt32("err", &err);
            finishSeek(err);

            if (err == OK) {
                int64_t timeUs;
                CHECK(msg->findInt64("time", &timeUs));
                mHandler->continueSeekAfterPause(timeUs);
            }
            break;
        }

        case MyHandler::kWhatAccessUnit:
        {
            size_t trackIndex;
            CHECK(msg->findSize("trackIndex", &trackIndex));

            if (mTSParser == NULL) {
                CHECK_LT(trackIndex, mTracks.size());
            } else {
                CHECK_EQ(trackIndex, 0u);
            }

            sp<ABuffer> accessUnit;
            CHECK(msg->findBuffer("accessUnit", &accessUnit));

            int32_t damaged;
            if (accessUnit->meta()->findInt32("damaged", &damaged)
                    && damaged) {
                ALOGI("dropping damaged access unit.");
                break;
            }

            if (mTSParser != NULL) {
                size_t offset = 0;
                status_t err = OK;
                while (offset + 188 <= accessUnit->size()) {
                    err = mTSParser->feedTSPacket(
                            accessUnit->data() + offset, 188);
                    if (err != OK) {
                        break;
                    }

                    offset += 188;
                }

                if (offset < accessUnit->size()) {
                    err = ERROR_MALFORMED;
                }

                if (err != OK) {
                    sp<AnotherPacketSource> source = getSource(false /* audio */);
                    if (source != NULL) {
                        source->signalEOS(err);
                    }

                    source = getSource(true /* audio */);
                    if (source != NULL) {
                        source->signalEOS(err);
                    }
                }
                break;
            }

            TrackInfo *info = &mTracks.editItemAt(trackIndex);

            sp<AnotherPacketSource> source = info->mSource;
            if (source != NULL) {
                uint32_t rtpTime;
                CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));

                if (!info->mNPTMappingValid) {
                    // This is a live stream, we didn't receive any normal
                    // playtime mapping. We won't map to npt time.
                    source->queueAccessUnit(accessUnit);
                    break;
                }

                int64_t nptUs =
                    ((double)rtpTime - (double)info->mRTPTime)
                        / info->mTimeScale
                        * 1000000ll
                        + info->mNormalPlaytimeUs;

                accessUnit->meta()->setInt64("timeUs", nptUs);

                source->queueAccessUnit(accessUnit);
            }
            break;
        }

        case MyHandler::kWhatEOS:
        {
            int32_t finalResult;
            CHECK(msg->findInt32("finalResult", &finalResult));
            CHECK_NE(finalResult, (status_t)OK);

            if (mTSParser != NULL) {
                sp<AnotherPacketSource> source = getSource(false /* audio */);
                if (source != NULL) {
                    source->signalEOS(finalResult);
                }

                source = getSource(true /* audio */);
                if (source != NULL) {
                    source->signalEOS(finalResult);
                }

                return;
            }

            size_t trackIndex;
            CHECK(msg->findSize("trackIndex", &trackIndex));
            CHECK_LT(trackIndex, mTracks.size());

            TrackInfo *info = &mTracks.editItemAt(trackIndex);
            sp<AnotherPacketSource> source = info->mSource;
            if (source != NULL) {
                source->signalEOS(finalResult);
            }

            break;
        }

        case MyHandler::kWhatSeekDiscontinuity:
        {
            size_t trackIndex;
            CHECK(msg->findSize("trackIndex", &trackIndex));
            CHECK_LT(trackIndex, mTracks.size());

            TrackInfo *info = &mTracks.editItemAt(trackIndex);
            sp<AnotherPacketSource> source = info->mSource;
            if (source != NULL) {
                source->queueDiscontinuity(
                        ATSParser::DISCONTINUITY_TIME,
                        NULL,
                        true /* discard */);
            }

            break;
        }

        case MyHandler::kWhatNormalPlayTimeMapping:
        {
            size_t trackIndex;
            CHECK(msg->findSize("trackIndex", &trackIndex));
            CHECK_LT(trackIndex, mTracks.size());

            uint32_t rtpTime;
            CHECK(msg->findInt32("rtpTime", (int32_t *)&rtpTime));

            int64_t nptUs;
            CHECK(msg->findInt64("nptUs", &nptUs));

            TrackInfo *info = &mTracks.editItemAt(trackIndex);
            info->mRTPTime = rtpTime;
            info->mNormalPlaytimeUs = nptUs;
            info->mNPTMappingValid = true;
            break;
        }

        case SDPLoader::kWhatSDPLoaded:
        {
            onSDPLoaded(msg);
            break;
        }

        default:
            TRESPASS();
    }
}
void ParseTreeNumeric::Initialize(const ConstituencyDictionary &dictionary,
                                  const ParseTree &parse_tree) {
  terminals_.clear();
  non_terminals_.clear();

  // Build a map from parse tree nodes to their indices in
  // terminal/non-terminals.
  std::map<ParseTreeNode*, int> map_terminals;
  std::map<ParseTreeNode*, int> map_non_terminals;
  for (int i = 0; i < parse_tree.terminals().size(); ++i) {
    map_terminals[parse_tree.terminals()[i]] = i;
  }
  for (int i = 0; i < parse_tree.non_terminals().size(); ++i) {
    map_non_terminals[parse_tree.non_terminals()[i]] = i;
  }

  terminals_.resize(parse_tree.terminals().size());
  non_terminals_.resize(parse_tree.non_terminals().size());
  for (int i = 0; i < parse_tree.terminals().size(); ++i) {
    terminals_[i] = new ParseTreeNumericNode;
  }
  for (int i = 0; i < parse_tree.non_terminals().size(); ++i) {
    non_terminals_[i] = new ParseTreeNumericNode;
  }
  for (int i = 0; i < parse_tree.terminals().size(); ++i) {
    ParseTreeNode *original_node = parse_tree.terminals()[i];
    ParseTreeNumericNode *node = terminals_[i];
    node->set_label(-1); // Terminal nodes receive no label.
    node->set_span(original_node->span());
    if (!original_node->parent()) {
      node->set_parent(NULL);
      root_ = node;
    } else {
      int parent_index = map_non_terminals[original_node->parent()];
      node->set_parent(non_terminals_[parent_index]);
    }
    CHECK_EQ(original_node->GetNumChildren(), 0);
  }

  for (int i = 0; i < parse_tree.non_terminals().size(); ++i) {
    ParseTreeNode *original_node = parse_tree.non_terminals()[i];
    ParseTreeNumericNode *node = non_terminals_[i];
    int id = dictionary.GetConstituentId(original_node->label());
    CHECK_LT(id, 0xffff);
    if (id < 0) id = TOKEN_UNKNOWN;
    node->set_label(id);
    node->set_span(original_node->span());
    if (!original_node->parent()) {
      node->set_parent(NULL);
      root_ = node;
    } else {
      int parent_index = map_non_terminals[original_node->parent()];
      node->set_parent(non_terminals_[parent_index]);
    }
    for (int j = 0; j < original_node->GetNumChildren(); ++j) {
      ParseTreeNode *child = original_node->GetChild(j);
      if (child->IsLeaf()) {
        int child_index = map_terminals[child];
        node->AddChild(terminals_[child_index]);
      } else {
        int child_index = map_non_terminals[child];
        node->AddChild(non_terminals_[child_index]);
      }
    }
  }
}
void ASessionDescription::getFormat(size_t index, AString *value) const {
    CHECK_GE(index, 0u);
    CHECK_LT(index, mTracks.size());

    *value = mFormats.itemAt(index);
}
void ConstituencyDictionary::CreateConstituentDictionary(
    ConstituencyReader *reader) {
  // Create tag dictionary.
  CreateTagDictionary(reader);

  // Create constituent dictionary.
  LOG(INFO) << "Creating constituent and rule dictionary...";
  std::vector<int> label_freqs(constituent_alphabet_.size(), -1);

  int lemma_cutoff = FLAGS_constituency_lemma_cutoff;
  int morph_cutoff = FLAGS_constituency_morph_cutoff;
  std::vector<int> lemma_freqs;
  std::vector<int> morph_freqs;
  Alphabet lemma_alphabet;
  Alphabet morph_alphabet;

  string special_symbols[NUM_SPECIAL_TOKENS];
  special_symbols[TOKEN_UNKNOWN] = kConstituencyTokenUnknown;
  special_symbols[TOKEN_START] = kConstituencyTokenStart;
  special_symbols[TOKEN_STOP] = kConstituencyTokenStop;

  for (int i = 0; i < NUM_SPECIAL_TOKENS; ++i) {
    lemma_alphabet.Insert(special_symbols[i]);
    morph_alphabet.Insert(special_symbols[i]);

    // Counts of special symbols are set to -1:
    lemma_freqs.push_back(-1);
    morph_freqs.push_back(-1);
  }


  // Go through the corpus and build the label dictionary,
  // counting the frequencies.
  reader->Open(pipe_->GetOptions()->GetTrainingFilePath());
  ConstituencyInstance *instance =
    static_cast<ConstituencyInstance*>(reader->GetNext());
  while (instance != NULL) {
    // Word-level elements.
    int instance_length = instance->size();
    for (int i = 0; i < instance_length; ++i) {
      int id;
      // Add lemma to alphabet.
      id = lemma_alphabet.Insert(instance->GetLemma(i));
      if (id >= lemma_freqs.size()) {
        CHECK_EQ(id, lemma_freqs.size());
        lemma_freqs.push_back(0);
      }
      ++lemma_freqs[id];

      // Add FEATS to alphabet.
      for (int j = 0; j < instance->GetNumMorphFeatures(i); ++j) {
        id = morph_alphabet.Insert(instance->GetMorphFeature(i,j));
        if (id >= morph_freqs.size()) {
          CHECK_EQ(id, morph_freqs.size());
          morph_freqs.push_back(0);
        }
        ++morph_freqs[id];
      }
    }

    // Tree-level elements.
    const ParseTree &tree = instance->GetParseTree();
    const std::vector<ParseTreeNode*> &non_terminals = tree.non_terminals();
    int num_nodes = non_terminals.size();
    for (int i = 0; i < num_nodes; ++i) {
      ParseTreeNode *node = non_terminals[i];
      const std::string &label = node->label();

      // Add constituent to alphabet.
      int id = constituent_alphabet_.Insert(label);
      if (id >= label_freqs.size()) {
        CHECK_EQ(id, label_freqs.size());
        label_freqs.push_back(0);
      }
      ++label_freqs[id];

      // Add rule to alphabet.
      if (!node->IsPreTerminal()) {
        std::string rule = label + ":";
        for (int j = 0; j < node->GetNumChildren(); ++j) {
          rule += " " + node->GetChild(j)->label();
        }
        int rule_id = rule_alphabet_.Insert(rule);
      }
    }
    delete instance;
    instance = static_cast<ConstituencyInstance*>(reader->GetNext());
  }
  reader->Close();
  constituent_alphabet_.StopGrowth();

  // Now adjust the cutoffs if necessary.
  while (true) {
    lemma_alphabet_.clear();
    for (int i = 0; i < NUM_SPECIAL_TOKENS; ++i) {
      lemma_alphabet_.Insert(special_symbols[i]);
    }
    for (Alphabet::iterator iter = lemma_alphabet.begin();
         iter != lemma_alphabet.end();
         ++iter) {
      if (lemma_freqs[iter->second] > lemma_cutoff) {
        lemma_alphabet_.Insert(iter->first);
      }
    }
    if (lemma_alphabet_.size() < kConstituencyMaxLemmaAlphabetSize) break;
    ++lemma_cutoff;
    LOG(INFO) << "Incrementing lemma cutoff to " << lemma_cutoff << "...";
  }

  while (true) {
    morph_alphabet_.clear();
    for (int i = 0; i < NUM_SPECIAL_TOKENS; ++i) {
      morph_alphabet_.Insert(special_symbols[i]);
    }
    for (Alphabet::iterator iter = morph_alphabet.begin();
         iter != morph_alphabet.end();
         ++iter) {
      if (morph_freqs[iter->second] > morph_cutoff) {
        morph_alphabet_.Insert(iter->first);
      }
    }
    if (morph_alphabet_.size() < kConstituencyMaxMorphAlphabetSize) break;
    ++morph_cutoff;
    LOG(INFO) << "Incrementing FEATS cutoff to " << morph_cutoff << "...";
  }

  lemma_alphabet_.StopGrowth();
  morph_alphabet_.StopGrowth();

  CHECK_LT(lemma_alphabet_.size(), 0xffff);
  CHECK_LT(morph_alphabet_.size(), 0xffff);

  LOG(INFO) << "Number of lemmas: " << lemma_alphabet_.size();
  LOG(INFO) << "Number of feats: " << morph_alphabet_.size();
  LOG(INFO) << "Number of constituent tags: " << constituent_alphabet_.size();
  LOG(INFO) << "Number of rules: " << rule_alphabet_.size();
  LOG(INFO) << "Constituent tags and their frequencies:";
  for (Alphabet::iterator it = constituent_alphabet_.begin();
       it != constituent_alphabet_.end(); ++it) {
    std::string label = it->first;
    int label_id = it->second;
    LOG(INFO) << label << "\t" << label_freqs[label_id];
  }
}
std::string SqlError::formatMessage(const std::string &sql_query) const {
  CHECK(!sql_query.empty()) << "SQL query is empty";
  CHECK_LT(sql_query.size(), static_cast<std::size_t>(std::numeric_limits<int>::max()));

  int error_line_number = line_number_;
  int error_column_number = column_number_;

  if (error_line_number == -1) {
    // If the error location is not available,
    // just append the error message directly.
    return std::string("ERROR: ").append(error_stream_.str()).append("\n");
  }

  DCHECK_GT(error_column_number, -1) << "Invalid column number";

  int current_line_number = 0;
  int current_line_begin_pos = 0;
  int current_line_end_pos = -1;

  // Find the ending index of the (<error_line_number>-1)-th line
  // of <sql_query>.
  if (current_line_number < error_line_number) {
    while (current_line_number < error_line_number) {
      current_line_begin_pos = sql_query.find('\n', current_line_begin_pos);
      DCHECK_GE(current_line_begin_pos, 0) << "Invalid line number";
      ++current_line_number;
      ++current_line_begin_pos;
    }
  }

  /*
   * The BISON may point the error to a position beyond the last line of
   * the SQL query. We move it to the position to the end of the last line.
   */
  if (current_line_begin_pos == static_cast<int>(sql_query.size()) && column_number_ == 0) {
    current_line_end_pos = current_line_begin_pos - 1;
    current_line_begin_pos = sql_query.rfind('\n', current_line_end_pos - 1) + 1;

    // Move the error line to the previous line.
    --error_line_number;
    error_column_number = current_line_end_pos - current_line_begin_pos;
  } else {
    current_line_end_pos = sql_query.find('\n', current_line_begin_pos);
    if (current_line_end_pos == -1) {
      current_line_end_pos = sql_query.size() - 1;
    }
    DCHECK(current_line_end_pos - current_line_begin_pos + 1 > column_number_) << "Invalid line and column number";
  }

  std::ostringstream error_stream;
  const int start_pos = getStartErrorPos(error_column_number + current_line_begin_pos, sql_query);
  const int end_pos = getEndErrorPos(error_column_number + current_line_begin_pos, sql_query);

  DCHECK_LE(start_pos, error_column_number + current_line_begin_pos);
  DCHECK_LE(error_column_number + current_line_begin_pos, end_pos);

  error_stream << "ERROR: " << getErrorMessage();
  error_stream << " (" << error_line_number + 1 << " : " << error_column_number + 1 << ")\n";

  // Append the snippet text.
  bool has_omitted_text = false;
  if (start_pos > current_line_begin_pos) {
    error_stream << "...";
    has_omitted_text = true;
  }
  error_stream << sql_query.substr(start_pos, end_pos - start_pos);
  if (end_pos < current_line_end_pos) {
    error_stream << "...";
  }
  error_stream << "\n";

  // Append a caret.
  if (has_omitted_text) {
    error_stream << "   ";
  }
  for (int i = start_pos; i < error_column_number + current_line_begin_pos; ++i) {
    if (sql_query.at(i) == '\t') {
      error_stream << "\t";
    } else {
      error_stream << " ";
    }
  }
  error_stream << "^\n";

  return error_stream.str();
}
Exemple #11
0
int32_t DenseOpLog::GetVecIndex(int32_t row_id) {
#ifdef PETUUM_CHECK_DENSE_STORAGE_RANGE
  CHECK_LT(row_id, capacity_);
#endif
  return row_id % capacity_;
}
Exemple #12
0
void MMSBModel::VIComputeGrads() {
  //float batch_scale 
  //    = vertices_.size() * (vertices_.size() - 1.0) 
  //      / (train_batch_vertices_.size() * (train_batch_vertices_.size() - 1.0));
  //TODO: temp
  //batch_scale = 1.0;
  float batch_scale = 0.5 * vertices_.size() / train_batch_vertices_.size();

  // Reset grads & intermidiate stats
  fill(lambda_grads_.begin(), lambda_grads_.end(), make_pair(0, 0));
  fill(bphi_sum_.begin(), bphi_sum_.end(), 0);
  fill(bphi_square_sum_.begin(), bphi_square_sum_.end(), 0);

  for (const auto i : train_batch_vertices_) {
    CHECK_LT(i, vertices_.size());
    float degree = vertices_[i]->degree();
    if (degree == 0) {
      continue;
    }
    //CHECK_GT(degree, 0) << i;
    const unordered_map<CIndex, Count>& z_cnts = vertices_[i]->z_cnts();
    for (const auto z_cnt : z_cnts) {
      CHECK_LT(z_cnt.first, K_);
      bphi_sum_[z_cnt.first] += z_cnt.second / degree;
      bphi_square_sum_[z_cnt.first] 
          += (z_cnt.second / degree) * (z_cnt.second / degree);
    }
  } // end of vertices

  for (const auto& link : train_batch_links_) {
    VIndex i = link.first;
    VIndex j = link.second;
    const unordered_map<CIndex, Count>& z_cnts_i = vertices_[i]->z_cnts();
    Vertex* v_j = vertices_[j];
    float degree_i = vertices_[i]->degree();
    float degree_j = v_j->degree();
    //TODO(zhiting) iterates i or j whose degree is smaller
    for (const auto z_cnt_i : z_cnts_i) { 
      CIndex z = z_cnt_i.first;
      lambda_grads_[z].second
          -= (z_cnt_i.second / degree_i) * (v_j->z_cnt(z) / degree_j);
    }
  } // end of links

  for (int k = 0; k < K_; ++k) {
    lambda_grads_[k].first += 2.0 * train_batch_k_cnts_[k]; // !!Caution

    CHECK(!isnan(lambda_grads_[k].first)) << k << "\t" << train_batch_k_cnts_[k] << "\t" << batch_scale;
    CHECK(!isinf(lambda_grads_[k].first)) << k << "\t" << train_batch_k_cnts_[k] << "\t" << batch_scale;

    lambda_grads_[k].second
        += (bphi_sum_[k] * bphi_sum_[k] - bphi_square_sum_[k]) / 2.0;

    CHECK(!isnan(lambda_grads_[k].second)) << k << "\t" << bphi_sum_[k] << "\t" << bphi_square_sum_[k];
  } 
  for (int k = 0; k < K_; ++k) {
    float temp = lambda_grads_[k].first;
    lambda_grads_[k].first = lambda_grads_[k].first * batch_scale + eta_.first;

    CHECK(!isnan(lambda_grads_[k].first)) << k << "\t" << train_batch_k_cnts_[k] << "\t" << temp << "\t" << batch_scale;
    CHECK(!isinf(lambda_grads_[k].first)) << k << "\t" << train_batch_k_cnts_[k] << "\t" << temp << "\t" << batch_scale;

    temp = lambda_grads_[k].second;
    lambda_grads_[k].second = lambda_grads_[k].second * batch_scale + eta_.second;

    CHECK(!isnan(lambda_grads_[k].second)) << k << "\t" << temp << "\t" << batch_scale;
    CHECK(!isinf(lambda_grads_[k].second)) << k << "\t" << temp << "\t" << batch_scale;
  }
}
void RTSPSource::onMessageReceived(const sp<AMessage> &msg) {
    if (msg->what() == kWhatDisconnect) {
        uint32_t replyID;
        CHECK(msg->senderAwaitsResponse(&replyID));

        mDisconnectReplyID = replyID;
        finishDisconnectIfPossible();
        return;
    } else if (msg->what() == kWhatPerformSeek) {
        int32_t generation;
        CHECK(msg->findInt32("generation", &generation));

        if (generation != mSeekGeneration) {
            // obsolete.
            return;
        }

        int64_t seekTimeUs;
        CHECK(msg->findInt64("timeUs", &seekTimeUs));

        performSeek(seekTimeUs);
        return;
    } else if (msg->what() == kWhatPerformPlay) {
        int64_t playTimeUs;
        CHECK(msg->findInt64("timeUs", &playTimeUs));
        performPlay(playTimeUs);
        return;
    } else if (msg->what() == kWhatPerformPause) {
        performPause();
        return;
    } else if (msg->what() == kWhatPerformResume) {
        performResume();
        return;
    } else if (msg->what() == kWhatPerformSuspend) {
        performSuspend();
        return;
    }

    CHECK_EQ(msg->what(), (uint32_t)kWhatNotify);

    int32_t what;
    int32_t isSeekable = 0;
    CHECK(msg->findInt32("what", &what));

    switch (what) {
        case RtspConnectionHandler::kWhatConnected:
            CHECK(msg->findInt32("isSeekable", &isSeekable));
            onConnected((isSeekable ? true:false));
            break;

        case RtspConnectionHandler::kWhatDisconnected:
            onDisconnected(msg);
            break;

        case RtspConnectionHandler::kWhatSeekDone:
        {
            mState = PLAYING;
            break;
        }

        case RtspConnectionHandler::kWhatPausedDone:
        {
            for (size_t i = 0; i < mTracks.size(); ++i) {
                TrackInfo *info = &mTracks.editItemAt(i);
                info->mLatestPausedUnit = info->mLatestReceivedUnit;
            }

            // The timestamp after a 'Pause' is done is the earliest
            // timestamp among all of the latest received units.
            TrackInfo *info = &mTracks.editItemAt(0);
            mLatestPausedUnit = info->mLatestReceivedUnit;
            for (size_t i = 1; i < mTracks.size(); ++i) {
                TrackInfo *info = &mTracks.editItemAt(i);
                if (mLatestPausedUnit > info->mLatestReceivedUnit) {
                    mLatestPausedUnit = info->mLatestReceivedUnit;
                }
            }
            break;
        }

        case RtspConnectionHandler::kWhatAccessUnit:
        {
            size_t trackIndex;
            CHECK(msg->findSize("trackIndex", &trackIndex));
            CHECK_LT(trackIndex, mTracks.size());

            sp<RefBase> obj;
            CHECK(msg->findObject("accessUnit", &obj));

            sp<ABuffer> accessUnit = static_cast<ABuffer *>(obj.get());

            int32_t damaged;
            if (accessUnit->meta()->findInt32("damaged", &damaged)
                    && damaged) {
                LOGI("dropping damaged access unit.");
                break;
            }

            TrackInfo *info = &mTracks.editItemAt(trackIndex);

            sp<AnotherPacketSource> source = info->mSource;
            if (source != NULL) {
                uint32_t rtpTime;
                CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));

                if (!info->mNPTMappingValid) {
                    // This is a live stream, we didn't receive any normal
                    // playtime mapping. Assume the first packets correspond
                    // to time 0.

                    LOGV("This is a live stream, assuming time = 0");

                    info->mRTPTime = rtpTime;
                    info->mNormalPlaytimeUs = 0ll;
                    info->mNPTMappingValid = true;
                }

                int64_t nptUs =
                    ((double)rtpTime - (double)info->mRTPTime)
                        / info->mTimeScale
                        * 1000000ll
                        + info->mNormalPlaytimeUs;

                accessUnit->meta()->setInt64("timeUs", nptUs);
                info->mLatestReceivedUnit = nptUs;
                // Drop the frames that are older than the frames in the queue.
                if (info->mLatestPausedUnit && (int64_t)info->mLatestPausedUnit > nptUs) {
                  break;
                }
                source->queueAccessUnit(accessUnit);
            }

            onTrackDataAvailable(trackIndex);
            break;
        }

        case RtspConnectionHandler::kWhatEOS:
        {
            size_t trackIndex;
            CHECK(msg->findSize("trackIndex", &trackIndex));
            CHECK_LT(trackIndex, mTracks.size());

            int32_t finalResult;
            CHECK(msg->findInt32("finalResult", &finalResult));
            CHECK_NE(finalResult, (status_t)OK);

            TrackInfo *info = &mTracks.editItemAt(trackIndex);
            sp<AnotherPacketSource> source = info->mSource;
            if (source != NULL) {
                source->signalEOS(finalResult);
            }

            break;
        }

        case RtspConnectionHandler::kWhatSeekDiscontinuity:
        {
            size_t trackIndex;
            CHECK(msg->findSize("trackIndex", &trackIndex));
            CHECK_LT(trackIndex, mTracks.size());

            TrackInfo *info = &mTracks.editItemAt(trackIndex);
            sp<AnotherPacketSource> source = info->mSource;
            if (source != NULL) {
                source->queueDiscontinuity(ATSParser::DISCONTINUITY_SEEK, NULL);
            }

            break;
        }

        case RtspConnectionHandler::kWhatNormalPlayTimeMapping:
        {
            size_t trackIndex;
            CHECK(msg->findSize("trackIndex", &trackIndex));
            CHECK_LT(trackIndex, mTracks.size());

            uint32_t rtpTime;
            CHECK(msg->findInt32("rtpTime", (int32_t *)&rtpTime));

            int64_t nptUs;
            CHECK(msg->findInt64("nptUs", &nptUs));

            TrackInfo *info = &mTracks.editItemAt(trackIndex);
            info->mRTPTime = rtpTime;
            info->mNormalPlaytimeUs = nptUs;
            info->mNPTMappingValid = true;
            break;
        }

        default:
            TRESPASS();
    }
}
// Reads an integer or a float from ptr based on the type and the byte width.
TargetValue ResultSet::makeTargetValue(const int8_t* ptr,
                                       const int8_t compact_sz,
                                       const TargetInfo& target_info,
                                       const size_t target_logical_idx,
                                       const bool translate_strings,
                                       const bool decimal_to_double,
                                       const size_t entry_buff_idx) const {
  const bool float_argument_input = takes_float_argument(target_info);
  const auto actual_compact_sz = float_argument_input ? sizeof(float) : compact_sz;

  auto ival = read_int_from_buff(ptr, actual_compact_sz);
  const auto& chosen_type = get_compact_type(target_info);
  if (!lazy_fetch_info_.empty()) {
    CHECK_LT(target_logical_idx, lazy_fetch_info_.size());
    const auto& col_lazy_fetch = lazy_fetch_info_[target_logical_idx];
    if (col_lazy_fetch.is_lazily_fetched) {
      const auto storage_idx = getStorageIndex(entry_buff_idx);
      CHECK_LT(static_cast<size_t>(storage_idx.first), col_buffers_.size());
      auto& frag_col_buffers = getColumnFrag(storage_idx.first, target_logical_idx, ival);
      ival = lazy_decode(col_lazy_fetch, frag_col_buffers[col_lazy_fetch.local_col_id], ival);
      if (chosen_type.is_fp()) {
        const auto dval = *reinterpret_cast<const double*>(may_alias_ptr(&ival));
        if (chosen_type.get_type() == kFLOAT) {
          return ScalarTargetValue(static_cast<float>(dval));
        } else {
          return ScalarTargetValue(dval);
        }
      }
    }
  }
  if (chosen_type.is_fp()) {
    switch (actual_compact_sz) {
      case 8: {
        const auto dval = *reinterpret_cast<const double*>(ptr);
        return chosen_type.get_type() == kFLOAT ? ScalarTargetValue(static_cast<const float>(dval))
                                                : ScalarTargetValue(dval);
      }
      case 4: {
        CHECK_EQ(kFLOAT, chosen_type.get_type());
        return *reinterpret_cast<const float*>(ptr);
      }
      default:
        CHECK(false);
    }
  }
  if (chosen_type.is_integer() | chosen_type.is_boolean() || chosen_type.is_time() || chosen_type.is_timeinterval()) {
    if (is_distinct_target(target_info)) {
      return TargetValue(
          count_distinct_set_size(ival, target_logical_idx, query_mem_desc_.count_distinct_descriptors_));
    }
    // TODO(alex): remove int_resize_cast, make read_int_from_buff return the right type instead
    if (inline_int_null_val(chosen_type) == int_resize_cast(ival, chosen_type.get_logical_size())) {
      return inline_int_null_val(target_info.sql_type);
    }
    return ival;
  }
  if (chosen_type.is_string() && chosen_type.get_compression() == kENCODING_DICT) {
    if (translate_strings) {
      if (static_cast<int32_t>(ival) == NULL_INT) {  // TODO(alex): this isn't nice, fix it
        return NullableString(nullptr);
      }
      StringDictionaryProxy* sdp{nullptr};
      if (!chosen_type.get_comp_param()) {
        sdp = row_set_mem_owner_->getLiteralStringDictProxy();
      } else {
        sdp = executor_ ? executor_->getStringDictionaryProxy(chosen_type.get_comp_param(), row_set_mem_owner_, false)
                        : row_set_mem_owner_->getStringDictProxy(chosen_type.get_comp_param());
      }
      return NullableString(sdp->getString(ival));
    } else {
      return static_cast<int64_t>(static_cast<int32_t>(ival));
    }
  }
  if (chosen_type.is_decimal()) {
    if (decimal_to_double) {
      if (ival == inline_int_null_val(SQLTypeInfo(decimal_to_int_type(chosen_type), false))) {
        return NULL_DOUBLE;
      }
      return static_cast<double>(ival) / exp_to_scale(chosen_type.get_scale());
    }
    return ival;
  }
  CHECK(false);
  return TargetValue(int64_t(0));
}
void DependencyDictionary::CreateLabelDictionary(DependencyReader *reader) {
  LOG(INFO) << "Creating label dictionary...";

  vector<int> label_freqs;

  // Go through the corpus and build the label dictionary,
  // counting the frequencies.
  reader->Open(file_train_);
  DependencyInstance *instance =
    static_cast<DependencyInstance*>(reader->GetNext());
  while (instance != NULL) {
    int instance_length = instance->size();
    for (int i = 1; i < instance_length; ++i) {
      int id;

      // Add dependency label to alphabet.
      id = label_alphabet_.Insert(instance->GetDependencyRelation(i));
      if (id >= label_freqs.size()) {
        CHECK_EQ(id, label_freqs.size());
        label_freqs.push_back(0);
      }
      ++label_freqs[id];
    }
    delete instance;
    instance = static_cast<DependencyInstance*>(reader->GetNext());
  }
  reader->Close();
  label_alphabet_.StopGrowth();

  // Go through the corpus and build the existing labels for each head-modifier
  // POS pair.
  existing_labels_.clear();
  existing_labels_.resize(token_dictionary_->GetNumPosTags(),
                          vector<vector<int> >(
                            token_dictionary_->GetNumPosTags()));

  maximum_left_distances_.clear();
  maximum_left_distances_.resize(token_dictionary_->GetNumPosTags(),
                                 vector<int>(
                                   token_dictionary_->GetNumPosTags(), 0));

  maximum_right_distances_.clear();
  maximum_right_distances_.resize(token_dictionary_->GetNumPosTags(),
                                  vector<int>(
                                    token_dictionary_->GetNumPosTags(), 0));

  reader->Open(file_train_);
  instance = static_cast<DependencyInstance*>(reader->GetNext());
  while (instance != NULL) {
    int instance_length = instance->size();
    for (int i = 1; i < instance_length; ++i) {
      int id;
      int head = instance->GetHead(i);
      CHECK_GE(head, 0);
      CHECK_LT(head, instance_length);
      const string &modifier_pos = instance->GetPosTag(i);
      const string &head_pos = instance->GetPosTag(head);
      int modifier_pos_id = token_dictionary_->GetPosTagId(modifier_pos);
      int head_pos_id = token_dictionary_->GetPosTagId(head_pos);
      if (modifier_pos_id < 0) modifier_pos_id = TOKEN_UNKNOWN;
      if (head_pos_id < 0) head_pos_id = TOKEN_UNKNOWN;
      //CHECK_GE(modifier_pos_id, 0);
      //CHECK_GE(head_pos_id, 0);

      id = label_alphabet_.Lookup(instance->GetDependencyRelation(i));
      CHECK_GE(id, 0);

      // Insert new label in the set of existing labels, if it is not there
      // already. NOTE: this is inefficient, maybe we should be using a 
      // different data structure.
      vector<int> &labels = existing_labels_[modifier_pos_id][head_pos_id];
      int j;
      for (j = 0; j < labels.size(); ++j) {
        if (labels[j] == id) break;
      }
      if (j == labels.size()) labels.push_back(id);

      // Update the maximum distances if necessary.
      if (head != 0) {
        if (head < i) {
          // Right attachment.
          if (i - head > maximum_right_distances_[modifier_pos_id][head_pos_id]) {
            maximum_right_distances_[modifier_pos_id][head_pos_id] = i - head;
          }
        } else {
          // Left attachment.
          if (head - i > maximum_left_distances_[modifier_pos_id][head_pos_id]) {
            maximum_left_distances_[modifier_pos_id][head_pos_id] = head - i;
          }
        }
      }
    }
    delete instance;
    instance = static_cast<DependencyInstance*>(reader->GetNext());
  }
  reader->Close();

  LOG(INFO) << "Number of labels: " << label_alphabet_.size();
}
void EntityDictionary::ReadGazetteerFiles() {
  EntityOptions *options =
    static_cast<EntityOptions*>(pipe_->GetOptions());
  gazetteer_case_sensitive_ = options->gazetteer_case_sensitive();

  gazetteer_word_alphabet_.AllowGrowth();
  gazetteer_entity_tag_alphabet_.AllowGrowth();

  if (options->file_gazetteer() != "") {
    LOG(INFO) << "Loading gazetteer file "
      << options->file_gazetteer() << "...";
    std::ifstream is;
    std::string line;

    // Do a first pass just to count the words and create the
    // dictionaries.
    is.open(options->file_gazetteer().c_str(), ifstream::in);
    CHECK(is.good()) << "Could not open "
      << options->file_gazetteer() << ".";
    if (is.is_open()) {
      while (!is.eof()) {
        getline(is, line);
        if (line == "") continue; // Ignore blank lines.
        std::vector<std::string> fields;
        StringSplit(line, " \t", &fields, true); // Break on tabs or spaces.
        if (fields.size() < 2) continue;
        const std::string &entity_type = fields[0];
        gazetteer_entity_tag_alphabet_.Insert("B-" + entity_type);
        gazetteer_entity_tag_alphabet_.Insert("I-" + entity_type);
        gazetteer_entity_tag_alphabet_.Insert("L-" + entity_type);
        gazetteer_entity_tag_alphabet_.Insert("U-" + entity_type);
        for (int k = 1; k < fields.size(); ++k) {
          if (!gazetteer_case_sensitive_) {
            std::transform(fields[k].begin(), fields[k].end(),
                           fields[k].begin(), ::tolower);
          }
          const std::string &word = fields[k];
          gazetteer_word_alphabet_.Insert(word);
        }
      }
    }
    is.close();

    // Now do the second pass to actually fill in the data.
    gazetteer_word_entity_tags_.clear();
    gazetteer_word_entity_tags_.resize(gazetteer_word_alphabet_.size());
    is.open(options->file_gazetteer().c_str(), ifstream::in);
    CHECK(is.good()) << "Could not open "
      << options->file_gazetteer() << ".";
    if (is.is_open()) {
      while (!is.eof()) {
        getline(is, line);
        if (line == "") continue; // Ignore blank lines.
        std::vector<std::string> fields;
        StringSplit(line, " \t", &fields, true); // Break on tabs or spaces.
        if (fields.size() < 2) continue;
        const std::string &entity_type = fields[0];
        int entity_type_begin_id =
          gazetteer_entity_tag_alphabet_.Lookup("B-" + entity_type);
        int entity_type_inside_id =
          gazetteer_entity_tag_alphabet_.Lookup("I-" + entity_type);
        int entity_type_last_id =
          gazetteer_entity_tag_alphabet_.Lookup("L-" + entity_type);
        int entity_type_unique_id =
          gazetteer_entity_tag_alphabet_.Lookup("U-" + entity_type);
        for (int k = 1; k < fields.size(); ++k) {
          if (!gazetteer_case_sensitive_) {
            std::transform(fields[k].begin(), fields[k].end(),
                           fields[k].begin(), ::tolower);
          }
          const std::string &word = fields[k];
          int word_id = gazetteer_word_alphabet_.Lookup(word);
          CHECK_GE(word_id, 0);
          CHECK_LT(word_id, gazetteer_word_entity_tags_.size());
          int entity_type_id = -1;
          if (fields.size() == 2) {
            entity_type_id = entity_type_unique_id;
          } else if (k == 1) {
            entity_type_id = entity_type_begin_id;
          } else if (k == fields.size() - 1) {
            entity_type_id = entity_type_last_id;
          } else {
            entity_type_id = entity_type_inside_id;
          }
          int l = -1;
          for (l = 0; l < gazetteer_word_entity_tags_[word_id].size(); ++l) {
            if (gazetteer_word_entity_tags_[word_id][l] == entity_type_id) {
              break;
            }
          }
          if (l == gazetteer_word_entity_tags_[word_id].size()) {
            gazetteer_word_entity_tags_[word_id].
              push_back(entity_type_id);
          }
        }
      }
    }
    is.close();
  }

  gazetteer_word_alphabet_.StopGrowth();
  gazetteer_entity_tag_alphabet_.StopGrowth();
  LOG(INFO) << "Number of gazetteer words: "
    << gazetteer_word_alphabet_.size();
  LOG(INFO) << "Number of gazetteer entity tags: "
    << gazetteer_entity_tag_alphabet_.size();
}
Exemple #17
0
 /// @brief returns the ids of the bottom blobs of layer i
 inline const vector<int> & bottom_ids(int i) const {
   CHECK_GE(i, 0) << "Invalid layer id";
   CHECK_LT(i, bottom_id_vecs_.size()) << "Invalid layer id";
   return bottom_id_vecs_[i];
 }
void EntityTokenDictionary::Initialize(EntityReader *reader) {
  SetTokenDictionaryFlagValues();
  LOG(INFO) << "Creating token dictionary...";

  std::vector<int> form_freqs;
  std::vector<int> form_lower_freqs;
  std::vector<int> shape_freqs;
  std::vector<int> pos_freqs;
  Alphabet form_alphabet;
  Alphabet form_lower_alphabet;
  Alphabet shape_alphabet;
  Alphabet pos_alphabet;

  std::string special_symbols[NUM_SPECIAL_TOKENS];
  special_symbols[TOKEN_UNKNOWN] = kTokenUnknown;
  special_symbols[TOKEN_START] = kTokenStart;
  special_symbols[TOKEN_STOP] = kTokenStop;

  for (int i = 0; i < NUM_SPECIAL_TOKENS; ++i) {
    prefix_alphabet_.Insert(special_symbols[i]);
    suffix_alphabet_.Insert(special_symbols[i]);
    form_alphabet.Insert(special_symbols[i]);
    form_lower_alphabet.Insert(special_symbols[i]);
    shape_alphabet.Insert(special_symbols[i]);
    pos_alphabet.Insert(special_symbols[i]);

    // Counts of special symbols are set to -1:
    form_freqs.push_back(-1);
    form_lower_freqs.push_back(-1);
    shape_freqs.push_back(-1);
    pos_freqs.push_back(-1);
  }

  // Go through the corpus and build the dictionaries,
  // counting the frequencies.
  reader->Open(pipe_->GetOptions()->GetTrainingFilePath());
  EntityInstance *instance =
    static_cast<EntityInstance*>(reader->GetNext());
  while (instance != NULL) {
    int instance_length = instance->size();
    for (int i = 0; i < instance_length; ++i) {
      int id;

      // Add form to alphabet.
      std::string form = instance->GetForm(i);
      std::string form_lower(form);
      std::transform(form_lower.begin(), form_lower.end(),
                     form_lower.begin(), ::tolower);
      if (!form_case_sensitive) form = form_lower;
      id = form_alphabet.Insert(form);
      if (id >= form_freqs.size()) {
        CHECK_EQ(id, form_freqs.size());
        form_freqs.push_back(0);
      }
      ++form_freqs[id];

      // Add lower-case form to the alphabet.
      id = form_lower_alphabet.Insert(form_lower);
      if (id >= form_lower_freqs.size()) {
        CHECK_EQ(id, form_lower_freqs.size());
        form_lower_freqs.push_back(0);
      }
      ++form_lower_freqs[id];

      // Add prefix/suffix to alphabet.
      std::string prefix = form.substr(0, prefix_length);
      id = prefix_alphabet_.Insert(prefix);
      int start = form.length() - suffix_length;
      if (start < 0) start = 0;
      std::string suffix = form.substr(start, suffix_length);
      id = suffix_alphabet_.Insert(suffix);

      // Add shape to alphabet.
      std::string shape;
      GetWordShape(instance->GetForm(i), &shape);
      id = shape_alphabet.Insert(shape);
      if (id >= shape_freqs.size()) {
        CHECK_EQ(id, shape_freqs.size());
        shape_freqs.push_back(0);
      }
      ++shape_freqs[id];

      // Add POS to alphabet.
      id = pos_alphabet.Insert(instance->GetPosTag(i));
      if (id >= pos_freqs.size()) {
        CHECK_EQ(id, pos_freqs.size());
        pos_freqs.push_back(0);
      }
      ++pos_freqs[id];
    }
    delete instance;
    instance = static_cast<EntityInstance*>(reader->GetNext());
  }
  reader->Close();

  // Now adjust the cutoffs if necessary.
  while (true) {
    form_alphabet_.clear();
    for (int i = 0; i < NUM_SPECIAL_TOKENS; ++i) {
      form_alphabet_.Insert(special_symbols[i]);
    }
    for (Alphabet::iterator iter = form_alphabet.begin();
    iter != form_alphabet.end();
      ++iter) {
      if (form_freqs[iter->second] > form_cutoff) {
        form_alphabet_.Insert(iter->first);
      }
    }
    if (form_alphabet_.size() < kMaxFormAlphabetSize) break;
    ++form_cutoff;
    LOG(INFO) << "Incrementing form cutoff to " << form_cutoff << "...";
  }

  while (true) {
    form_lower_alphabet_.clear();
    for (int i = 0; i < NUM_SPECIAL_TOKENS; ++i) {
      form_lower_alphabet_.Insert(special_symbols[i]);
    }
    for (Alphabet::iterator iter = form_lower_alphabet.begin();
    iter != form_lower_alphabet.end();
      ++iter) {
      if (form_lower_freqs[iter->second] > form_lower_cutoff) {
        form_lower_alphabet_.Insert(iter->first);
      }
    }
    if (form_lower_alphabet_.size() < kMaxFormAlphabetSize) break;
    ++form_lower_cutoff;
    LOG(INFO) << "Incrementing lower-case form cutoff to "
      << form_lower_cutoff << "...";
  }

  while (true) {
    shape_alphabet_.clear();
    for (int i = 0; i < NUM_SPECIAL_TOKENS; ++i) {
      shape_alphabet_.Insert(special_symbols[i]);
    }
    for (Alphabet::iterator iter = shape_alphabet.begin();
    iter != shape_alphabet.end();
      ++iter) {
      if (shape_freqs[iter->second] > shape_cutoff) {
        shape_alphabet_.Insert(iter->first);
      }
    }
    if (shape_alphabet_.size() < kMaxShapeAlphabetSize) break;
    ++shape_cutoff;
    LOG(INFO) << "Incrementing shape cutoff to " << shape_cutoff << "...";
  }

  while (true) {
    pos_alphabet_.clear();
    for (int i = 0; i < NUM_SPECIAL_TOKENS; ++i) {
      pos_alphabet_.Insert(special_symbols[i]);
    }
    for (const auto& pos_token : pos_alphabet) {
      if (pos_freqs[pos_token.second] > pos_cutoff) {
        pos_alphabet_.Insert(pos_token.first);
      }
    }
    if (pos_alphabet_.size() < kMaxPosAlphabetSize) break;
    ++pos_cutoff;
    LOG(INFO) << "Incrementing POS cutoff to " << pos_cutoff << "...";
  }

  form_alphabet_.StopGrowth();
  form_lower_alphabet_.StopGrowth();
  shape_alphabet_.StopGrowth();
  lemma_alphabet_.StopGrowth();
  prefix_alphabet_.StopGrowth();
  suffix_alphabet_.StopGrowth();
  feats_alphabet_.StopGrowth();
  pos_alphabet_.StopGrowth();
  cpos_alphabet_.StopGrowth();

  LOG(INFO) << "Number of forms: " << form_alphabet_.size() << endl
    << "Number of lower-case forms: " << form_lower_alphabet_.size() << endl
    << "Number of prefixes: " << prefix_alphabet_.size() << endl
    << "Number of suffixes: " << suffix_alphabet_.size() << endl
    << "Number of word shapes: " << shape_alphabet_.size() << endl
    << "Number of pos: " << pos_alphabet_.size();

  CHECK_LT(form_alphabet_.size(), 0xffff);
  CHECK_LT(form_lower_alphabet_.size(), 0xffff);
  CHECK_LT(shape_alphabet_.size(), 0xffff);
  CHECK_LT(lemma_alphabet_.size(), 0xffff);
  CHECK_LT(prefix_alphabet_.size(), 0xffff);
  CHECK_LT(suffix_alphabet_.size(), 0xffff);
  CHECK_LT(feats_alphabet_.size(), 0xffff);
  CHECK_LT(pos_alphabet_.size(), 0xff);
  CHECK_LT(cpos_alphabet_.size(), 0xff);

#ifndef NDEBUG
  BuildNames();
#endif
}
Exemple #19
0
void TrackerTrainer::Train(const cv::Mat& image_prev, const cv::Mat& image_curr,
                           const BoundingBox& bbox_prev, const BoundingBox& bbox_curr) {
  // Check that the saved batches are of appropriate dimensions.
  CHECK_EQ(images_batch_.size(), targets_batch_.size())
      << " images_batch: " << images_batch_.size() <<
         " targets_batch: " << targets_batch_.size();

  CHECK_EQ(images_batch_.size(), bboxes_gt_scaled_batch_.size())
      << " images_batch: " << images_batch_.size() <<
         " bboxes_gt_scaled_batch_: " << bboxes_gt_scaled_batch_.size();

  // We don't currently handle the case in which the number of generated examples per image is larger than the
  // batch size, so for now, throw an error if kGeneratedExamplesPerImage >= kBatchSize
  CHECK_LT(kGeneratedExamplesPerImage, kBatchSize);

  // Set up example generator.
  example_generator_->Reset(bbox_prev,
                           bbox_curr,
                           image_prev,
                           image_curr);

  // Make training examples.
  std::vector<cv::Mat> images;
  std::vector<cv::Mat> targets;
  std::vector<BoundingBox> bboxes_gt_scaled;
  MakeTrainingExamples(&images, &targets, &bboxes_gt_scaled);

  // Compute the number of images left to complete the batch.
  const int num_in_batch = images_batch_.size();
  const int num_left_in_batch = kBatchSize - num_in_batch;

  // The number of images to use is upper-bounded by the number left in the batch.
  // The rest go into the next batch.
  const int num_use = std::min(static_cast<int>(images.size()), num_left_in_batch);
  const int num_unused = images.size() - num_use;

  if (num_use < 0) {
    printf("Error: num_use: %d\n", num_use);
  }

  // Add the approrpriate number of images to the batch.
  images_batch_.insert(images_batch_.end(),
                       images.begin(), images.begin() + num_use);
  targets_batch_.insert(targets_batch_.end(),
                        targets.begin(), targets.begin() + num_use);
  bboxes_gt_scaled_batch_.insert(bboxes_gt_scaled_batch_.end(),
                                 bboxes_gt_scaled.begin(),
                                 bboxes_gt_scaled.begin() + num_use);

  // If we have a full batch, then train!
  if (images_batch_.size() == kBatchSize) {    
    // Increment the batch count.
    num_batches_++;

    // We have filled up a complete batch, so we should train.
    ProcessBatch();

    // After training, clear the batch.
    images_batch_.clear();
    targets_batch_.clear();
    bboxes_gt_scaled_batch_.clear();

    // Reserve the appropriate amount of space for the next batch.
    images_batch_.reserve(kBatchSize);
    targets_batch_.reserve(kBatchSize);
    bboxes_gt_scaled_batch_.reserve(kBatchSize);
  }

  // The remaining generated examples that were not used begin to fill
  // up the next batch.
  if (num_unused > 0) {
    images_batch_.insert(images_batch_.end(),
                         images.begin() + num_use, images.end());
    targets_batch_.insert(targets_batch_.end(),
                          targets.begin() + num_use, targets.end());
    bboxes_gt_scaled_batch_.insert(bboxes_gt_scaled_batch_.end(),
                                   bboxes_gt_scaled.begin() + num_use,
                                   bboxes_gt_scaled.end());
  }
}
Exemple #20
0
void RTSPSource::onMessageReceived(const sp<AMessage> &msg) {
    if (msg->what() == kWhatDisconnect) {
        uint32_t replyID;
        CHECK(msg->senderAwaitsResponse(&replyID));

        mDisconnectReplyID = replyID;
        finishDisconnectIfPossible();
        return;
    } else if (msg->what() == kWhatPerformSeek) {
        int32_t generation;
        CHECK(msg->findInt32("generation", &generation));

        if (generation != mSeekGeneration) {
            // obsolete.
            return;
        }

        int64_t seekTimeUs;
        CHECK(msg->findInt64("timeUs", &seekTimeUs));

        performSeek(seekTimeUs);
        return;
    } else if (msg->what() == kWhatPerformPlay) {
        int64_t playTimeUs;
        CHECK(msg->findInt64("timeUs", &playTimeUs));
        performPlay(playTimeUs);
        return;
    } else if (msg->what() == kWhatPerformPause) {
        performPause();
        return;
    } else if (msg->what() == kWhatPerformResume) {
        performResume();
        return;
    } else if (msg->what() == kWhatPerformSuspend) {
        performSuspend();
        return;
    } else if (msg->what() == kWhatPerformPlaybackEnded) {
        performPlaybackEnded();
        return;
    }

    CHECK_EQ(msg->what(), (uint32_t)kWhatNotify);

    int32_t what;
    int32_t isSeekable = 0;
    CHECK(msg->findInt32("what", &what));

    switch (what) {
        case RtspConnectionHandler::kWhatConnected:
            CHECK(msg->findInt32("isSeekable", &isSeekable));
            onConnected((isSeekable ? true:false));
            break;

        case RtspConnectionHandler::kWhatDisconnected:
            onDisconnected(msg);
            break;

        case RtspConnectionHandler::kWhatSeekDone:
        {
            mState = PLAYING;
            // Even if we have reset mLatestPausedUnit in performSeek(),
            // it's still possible that kWhatPausedDone event may arrive
            // because of previous performPause() command.
            for (size_t i = 0; i < mTracks.size(); ++i) {
                TrackInfo *info = &mTracks.editItemAt(i);
                info->mLatestPausedUnit = 0;
            }
            mLatestPausedUnit = 0;
            break;
        }

        case RtspConnectionHandler::kWhatPausedDone:
        {
            // Reject invalid state transition.
            if (mState != PAUSING) {
                return;
            }
            mState = PAUSED;

            for (size_t i = 0; i < mTracks.size(); ++i) {
                TrackInfo *info = &mTracks.editItemAt(i);
                info->mLatestPausedUnit = info->mLatestReceivedUnit;
            }

            // The timestamp after a 'Pause' is done is the earliest
            // timestamp among all of the latest received units.
            TrackInfo *info = &mTracks.editItemAt(0);
            mLatestPausedUnit = info->mLatestReceivedUnit;
            for (size_t i = 1; i < mTracks.size(); ++i) {
                TrackInfo *info = &mTracks.editItemAt(i);
                if (mLatestPausedUnit > info->mLatestReceivedUnit) {
                    mLatestPausedUnit = info->mLatestReceivedUnit;
                }
            }

            if (mPlayPending) {
                mPlayPending = false;
                performPlay(mLatestPausedUnit);
            }
            break;
        }

        case RtspConnectionHandler::kWhatAccessUnitComplete:
        {
            if (!isValidState()) {
                LOGI("We're disconnected, dropping access unit.");
                break;
            }
            size_t trackIndex;
            CHECK(msg->findSize("trackIndex", &trackIndex));
            CHECK_LT(trackIndex, mTracks.size());

            sp<RefBase> obj;
            CHECK(msg->findObject("accessUnit", &obj));

            sp<ABuffer> accessUnit = static_cast<ABuffer *>(obj.get());

            int32_t damaged;
            if (accessUnit->meta()->findInt32("damaged", &damaged)
                    && damaged) {
                LOGI("dropping damaged access unit.");
                break;
            }

            TrackInfo *info = &mTracks.editItemAt(trackIndex);

            sp<AnotherPacketSource> source = info->mSource;
            if (source != NULL) {
                uint32_t rtpTime;
                CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));

                if (!info->mNPTMappingValid) {
                    // This is a live stream, we didn't receive any normal
                    // playtime mapping. Assume the first packets correspond
                    // to time 0.

                    LOGV("This is a live stream, assuming time = 0");

                    info->mRTPTime = rtpTime;
                    info->mNormalPlaytimeUs = 0ll;
                    info->mNPTMappingValid = true;
                }

                int64_t nptUs =
                    ((double)rtpTime - (double)info->mRTPTime)
                        / info->mTimeScale
                        * 1000000ll
                        + info->mNormalPlaytimeUs;

                accessUnit->meta()->setInt64("timeUs", nptUs);
                info->mLatestReceivedUnit = nptUs;
                // Drop the frames that are older than the frames in the queue.
                if (info->mLatestPausedUnit && (int64_t)info->mLatestPausedUnit > nptUs) {
                    break;
                }
                source->queueAccessUnit(accessUnit);
            }

            onTrackDataAvailable(trackIndex);
            break;
        }

        case RtspConnectionHandler::kWhatEOS:
        {
            if (!isValidState()) {
                LOGI("We're disconnected, dropping end-of-stream message.");
                break;
            }
            size_t trackIndex;
            CHECK(msg->findSize("trackIndex", &trackIndex));
            CHECK_LT(trackIndex, mTracks.size());

            int32_t finalResult;
            CHECK(msg->findInt32("finalResult", &finalResult));
            CHECK_NE(finalResult, (status_t)OK);

            TrackInfo *info = &mTracks.editItemAt(trackIndex);
            sp<AnotherPacketSource> source = info->mSource;
            if (source != NULL) {
                source->signalEOS(finalResult);
            }

            onTrackEndOfStream(trackIndex);
            break;
        }

        case RtspConnectionHandler::kWhatSeekDiscontinuity:
        {
            if (!isValidState()) {
                LOGI("We're disconnected, dropping seek discontinuity message.");
                break;
            }
            size_t trackIndex;
            CHECK(msg->findSize("trackIndex", &trackIndex));
            CHECK_LT(trackIndex, mTracks.size());

            TrackInfo *info = &mTracks.editItemAt(trackIndex);
            sp<AnotherPacketSource> source = info->mSource;
            if (source != NULL) {
#if ANDROID_VERSION >= 21
                source->queueDiscontinuity(ATSParser::DISCONTINUITY_TIME, NULL,
                                           true /* discard */);
#else
                source->queueDiscontinuity(ATSParser::DISCONTINUITY_SEEK, NULL);
#endif
            }

            break;
        }

        case RtspConnectionHandler::kWhatNormalPlayTimeMapping:
        {
            if (!isValidState()) {
                LOGI("We're disconnected, dropping normal play time mapping "
                     "message.");
                break;
            }
            size_t trackIndex;
            CHECK(msg->findSize("trackIndex", &trackIndex));
            CHECK_LT(trackIndex, mTracks.size());

            uint32_t rtpTime;
            CHECK(msg->findInt32("rtpTime", (int32_t *)&rtpTime));

            int64_t nptUs;
            CHECK(msg->findInt64("nptUs", &nptUs));

            TrackInfo *info = &mTracks.editItemAt(trackIndex);
            info->mRTPTime = rtpTime;
            info->mNormalPlaytimeUs = nptUs;
            info->mNPTMappingValid = true;
            break;
        }

        case RtspConnectionHandler::kWhatTryTCPInterleaving:
        {
            // By default, we will request to deliver RTP over UDP. If the play
            // request timed out and we didn't receive any RTP packet, we will
            // fail back to use RTP interleaved in the existing RTSP/TCP
            // connection. And in this case, we have to explicitly perform
            // another play event to request the server to start streaming
            // again.
            int64_t playTimeUs;
            if (!msg->findInt64("timeUs", &playTimeUs)) {
                playTimeUs = 0;
            }
            performPlay(playTimeUs);
            break;
        }

        default:
            TRESPASS();
    }
}
static int IsEOL(const unsigned char* offset, const unsigned char* end)
{
	CHECK_LT(offset, end);
	return(*offset & 0x80) != 0;
}
void ScaleRouteLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
    const vector<Blob<Dtype>*>& top) {
  ScaleRouteParameter scale_route_param = this->layer_param_.scale_route_param();
  float low_scale, high_scale, mid_scale;
  if (scale_route_param.has_low_scale()) {
    low_scale = scale_route_param.low_scale();
    CHECK_GT(low_scale,0);
  } else {
    low_scale = 0.;
  }
  if (scale_route_param.has_high_scale()) {
    high_scale = scale_route_param.high_scale();
    CHECK_LT(high_scale,FLT_MAX);
  } else {
    high_scale = FLT_MAX;
  }
  if (scale_route_param.has_low_scale() && scale_route_param.has_high_scale()) {
    mid_scale = sqrt(low_scale*high_scale);
  } else if (scale_route_param.has_low_scale()) {
    mid_scale = low_scale;
  } else if (scale_route_param.has_high_scale()) {
    mid_scale = high_scale;
  } else {
    mid_scale = 256.;
  }
  CHECK_GE(high_scale,low_scale); CHECK_GT(mid_scale,0);
  
  const int num_rois = bottom[0]->num();
  const Dtype* rois_data = bottom[0]->cpu_data();
  const int rois_dim = bottom[0]->channels();
  vector<int> select_index;
  float min_mid_dist = FLT_MAX; int min_mid_idx = -1;
  for (int i = 0; i < num_rois; i++) {
    BBox bb = InitBBox(rois_data[i*rois_dim+1],rois_data[i*rois_dim+2],
            rois_data[i*rois_dim+3],rois_data[i*rois_dim+4]);
    float bb_size = BBoxSize(bb);
    float bb_scale = sqrt(bb_size);
    if (bb_scale>=low_scale && bb_scale<high_scale) {
      select_index.push_back(i);
    }
    float mid_dist = std::abs(log2(bb_scale/mid_scale));
    if (mid_dist<min_mid_dist) {
      min_mid_dist = mid_dist;
      min_mid_idx = i;
    }
  }
  // in case of no selected index
  if (select_index.size() == 0) {
    DLOG(INFO) <<"layer: "<<this->layer_param().name()<<", No samples between : "
            <<low_scale<<" and "<<high_scale<<"!!!";
    CHECK_GE(min_mid_idx,0);
    select_index.push_back(min_mid_idx);
  }
 
  // copy bottoms to tops
  const int select_num = select_index.size();
  for (int k = 0; k < top.size(); k++) {
    const int channels = bottom[k]->channels();
    const int height = bottom[k]->height();
    const int width = bottom[k]->width();
    const int dim = bottom[k]->count() / bottom[k]->num();   
    top[k]->Reshape(select_num, channels, height, width);
    
    const Dtype* bottom_data = bottom[k]->cpu_data();
    Dtype* top_data = top[k]->mutable_cpu_data();
    for (int i = 0; i < select_num; i++) {
      int idx = select_index[i]; CHECK_GT(bottom[k]->num(),idx);
      caffe_copy(dim, bottom_data+idx*dim, top_data+i*dim);
    }
  }
}
void WifiDisplaySource::onMessageReceived(const sp<AMessage> &msg) {
    switch (msg->what()) {
        case kWhatStart:
        {
            uint32_t replyID;
            CHECK(msg->senderAwaitsResponse(&replyID));

            AString iface;
            CHECK(msg->findString("iface", &iface));

            status_t err = OK;

            ssize_t colonPos = iface.find(":");

            unsigned long port;

            if (colonPos >= 0) {
                const char *s = iface.c_str() + colonPos + 1;

                char *end;
                port = strtoul(s, &end, 10);

                if (end == s || *end != '\0' || port > 65535) {
                    err = -EINVAL;
                } else {
                    iface.erase(colonPos, iface.size() - colonPos);
                }
            } else {
                port = kWifiDisplayDefaultPort;
            }

            if (err == OK) {
                if (inet_aton(iface.c_str(), &mInterfaceAddr) != 0) {
                    sp<AMessage> notify = new AMessage(kWhatRTSPNotify, id());

                    err = mNetSession->createRTSPServer(
                            mInterfaceAddr, port, notify, &mSessionID);
                } else {
                    err = -EINVAL;
                }
            }

            mState = AWAITING_CLIENT_CONNECTION;

            sp<AMessage> response = new AMessage;
            response->setInt32("err", err);
            response->postReply(replyID);
            break;
        }

        case kWhatRTSPNotify:
        {
            int32_t reason;
            CHECK(msg->findInt32("reason", &reason));

            switch (reason) {
                case ANetworkSession::kWhatError:
                {
                    int32_t sessionID;
                    CHECK(msg->findInt32("sessionID", &sessionID));

                    int32_t err;
                    CHECK(msg->findInt32("err", &err));

                    AString detail;
                    CHECK(msg->findString("detail", &detail));

                    ALOGE("An error occurred in session %d (%d, '%s/%s').",
                          sessionID,
                          err,
                          detail.c_str(),
                          strerror(-err));

                    mNetSession->destroySession(sessionID);

                    if (sessionID == mClientSessionID) {
                        mClientSessionID = 0;

                        mClient->onDisplayError(
                                IRemoteDisplayClient::kDisplayErrorUnknown);
                    }
                    break;
                }

                case ANetworkSession::kWhatClientConnected:
                {
                    int32_t sessionID;
                    CHECK(msg->findInt32("sessionID", &sessionID));

                    if (mClientSessionID > 0) {
                        ALOGW("A client tried to connect, but we already "
                              "have one.");

                        mNetSession->destroySession(sessionID);
                        break;
                    }

                    CHECK_EQ(mState, AWAITING_CLIENT_CONNECTION);

                    CHECK(msg->findString("client-ip", &mClientInfo.mRemoteIP));
                    CHECK(msg->findString("server-ip", &mClientInfo.mLocalIP));

                    if (mClientInfo.mRemoteIP == mClientInfo.mLocalIP) {
                        // Disallow connections from the local interface
                        // for security reasons.
                        mNetSession->destroySession(sessionID);
                        break;
                    }

                    CHECK(msg->findInt32(
                                "server-port", &mClientInfo.mLocalPort));
                    mClientInfo.mPlaybackSessionID = -1;

                    mClientSessionID = sessionID;

                    ALOGI("We now have a client (%d) connected.", sessionID);

                    mState = AWAITING_CLIENT_SETUP;

                    status_t err = sendM1(sessionID);
                    CHECK_EQ(err, (status_t)OK);
                    break;
                }

                case ANetworkSession::kWhatData:
                {
                    status_t err = onReceiveClientData(msg);

                    if (err != OK) {
                        mClient->onDisplayError(
                                IRemoteDisplayClient::kDisplayErrorUnknown);
                    }

#if 0
                    // testing only.
                    char val[PROPERTY_VALUE_MAX];
                    if (property_get("media.wfd.trigger", val, NULL)) {
                        if (!strcasecmp(val, "pause") && mState == PLAYING) {
                            mState = PLAYING_TO_PAUSED;
                            sendTrigger(mClientSessionID, TRIGGER_PAUSE);
                        } else if (!strcasecmp(val, "play")
                                    && mState == PAUSED) {
                            mState = PAUSED_TO_PLAYING;
                            sendTrigger(mClientSessionID, TRIGGER_PLAY);
                        }
                    }
#endif
                    break;
                }

                case ANetworkSession::kWhatNetworkStall:
                {
                    break;
                }

                default:
                    TRESPASS();
            }
            break;
        }

        case kWhatStop:
        {
            CHECK(msg->senderAwaitsResponse(&mStopReplyID));

            CHECK_LT(mState, AWAITING_CLIENT_TEARDOWN);

            if (mState >= AWAITING_CLIENT_PLAY) {
                // We have a session, i.e. a previous SETUP succeeded.

                status_t err = sendTrigger(
                        mClientSessionID, TRIGGER_TEARDOWN);

                if (err == OK) {
                    mState = AWAITING_CLIENT_TEARDOWN;

                    (new AMessage(kWhatTeardownTriggerTimedOut, id()))->post(
                            kTeardownTriggerTimeouSecs * 1000000ll);

                    break;
                }

                // fall through.
            }

            finishStop();
            break;
        }

        case kWhatPause:
        {
            uint32_t replyID;
            CHECK(msg->senderAwaitsResponse(&replyID));

            status_t err = OK;

            if (mState != PLAYING) {
                err = INVALID_OPERATION;
            } else {
                mState = PLAYING_TO_PAUSED;
                sendTrigger(mClientSessionID, TRIGGER_PAUSE);
            }

            sp<AMessage> response = new AMessage;
            response->setInt32("err", err);
            response->postReply(replyID);
            break;
        }

        case kWhatResume:
        {
            uint32_t replyID;
            CHECK(msg->senderAwaitsResponse(&replyID));

            status_t err = OK;

            if (mState != PAUSED) {
                err = INVALID_OPERATION;
            } else {
                mState = PAUSED_TO_PLAYING;
                sendTrigger(mClientSessionID, TRIGGER_PLAY);
            }

            sp<AMessage> response = new AMessage;
            response->setInt32("err", err);
            response->postReply(replyID);
            break;
        }

        case kWhatReapDeadClients:
        {
            mReaperPending = false;

            if (mClientSessionID == 0
                    || mClientInfo.mPlaybackSession == NULL) {
                break;
            }

            if (mClientInfo.mPlaybackSession->getLastLifesignUs()
                    + kPlaybackSessionTimeoutUs < ALooper::GetNowUs()) {
                ALOGI("playback session timed out, reaping.");

                mNetSession->destroySession(mClientSessionID);
                mClientSessionID = 0;

                mClient->onDisplayError(
                        IRemoteDisplayClient::kDisplayErrorUnknown);
            } else {
                scheduleReaper();
            }
            break;
        }

        case kWhatPlaybackSessionNotify:
        {
            int32_t playbackSessionID;
            CHECK(msg->findInt32("playbackSessionID", &playbackSessionID));

            int32_t what;
            CHECK(msg->findInt32("what", &what));

            if (what == PlaybackSession::kWhatSessionDead) {
                ALOGI("playback session wants to quit.");

                mClient->onDisplayError(
                        IRemoteDisplayClient::kDisplayErrorUnknown);
            } else if (what == PlaybackSession::kWhatSessionEstablished) {
                mPlaybackSessionEstablished = true;

                if (mClient != NULL) {
                    if (!mSinkSupportsVideo) {
                        mClient->onDisplayConnected(
                                NULL,  // SurfaceTexture
                                0, // width,
                                0, // height,
                                mUsingHDCP
                                    ? IRemoteDisplayClient::kDisplayFlagSecure
                                    : 0,
                                0);
                    } else {
                        size_t width, height;

                        CHECK(VideoFormats::GetConfiguration(
                                    mChosenVideoResolutionType,
                                    mChosenVideoResolutionIndex,
                                    &width,
                                    &height,
                                    NULL /* framesPerSecond */,
                                    NULL /* interlaced */));

                        mClient->onDisplayConnected(
                                mClientInfo.mPlaybackSession
                                    ->getSurfaceTexture(),
                                width,
                                height,
                                mUsingHDCP
                                    ? IRemoteDisplayClient::kDisplayFlagSecure
                                    : 0,
                                playbackSessionID);
                    }
                }

                finishPlay();

                if (mState == ABOUT_TO_PLAY) {
                    mState = PLAYING;
                }
            } else if (what == PlaybackSession::kWhatSessionDestroyed) {
                disconnectClient2();
            } else {
                CHECK_EQ(what, PlaybackSession::kWhatBinaryData);

                int32_t channel;
                CHECK(msg->findInt32("channel", &channel));

                sp<ABuffer> data;
                CHECK(msg->findBuffer("data", &data));

                CHECK_LE(channel, 0xffu);
                CHECK_LE(data->size(), 0xffffu);

                int32_t sessionID;
                CHECK(msg->findInt32("sessionID", &sessionID));

                char header[4];
                header[0] = '$';
                header[1] = channel;
                header[2] = data->size() >> 8;
                header[3] = data->size() & 0xff;

                mNetSession->sendRequest(
                        sessionID, header, sizeof(header));

                mNetSession->sendRequest(
                        sessionID, data->data(), data->size());
            }
            break;
        }

        case kWhatKeepAlive:
        {
            int32_t sessionID;
            CHECK(msg->findInt32("sessionID", &sessionID));

            if (mClientSessionID != sessionID) {
                // Obsolete event, client is already gone.
                break;
            }

            sendM16(sessionID);
            break;
        }

        case kWhatTeardownTriggerTimedOut:
        {
            if (mState == AWAITING_CLIENT_TEARDOWN) {
                ALOGI("TEARDOWN trigger timed out, forcing disconnection.");

                CHECK_NE(mStopReplyID, 0);
                finishStop();
                break;
            }
            break;
        }

        case kWhatHDCPNotify:
        {
            int32_t msgCode, ext1, ext2;
            CHECK(msg->findInt32("msg", &msgCode));
            CHECK(msg->findInt32("ext1", &ext1));
            CHECK(msg->findInt32("ext2", &ext2));

            ALOGI("Saw HDCP notification code %d, ext1 %d, ext2 %d",
                    msgCode, ext1, ext2);

            switch (msgCode) {
                case HDCPModule::HDCP_INITIALIZATION_COMPLETE:
                {
                    mHDCPInitializationComplete = true;

                    if (mSetupTriggerDeferred) {
                        mSetupTriggerDeferred = false;

                        sendTrigger(mClientSessionID, TRIGGER_SETUP);
                    }
                    break;
                }

                case HDCPModule::HDCP_SHUTDOWN_COMPLETE:
                case HDCPModule::HDCP_SHUTDOWN_FAILED:
                {
                    // Ugly hack to make sure that the call to
                    // HDCPObserver::notify is completely handled before
                    // we clear the HDCP instance and unload the shared
                    // library :(
                    (new AMessage(kWhatFinishStop2, id()))->post(300000ll);
                    break;
                }

                default:
                {
                    ALOGE("HDCP failure, shutting down.");

                    mClient->onDisplayError(
                            IRemoteDisplayClient::kDisplayErrorUnknown);
                    break;
                }
            }
            break;
        }

        case kWhatFinishStop2:
        {
            finishStop2();
            break;
        }

        default:
            TRESPASS();
    }
}
Exemple #24
0
Try<Nothing> LevelDBStorage::persist(const Action& action)
{
  Stopwatch stopwatch;
  stopwatch.start();

  Record record;
  record.set_type(Record::ACTION);
  record.mutable_action()->MergeFrom(action);

  string value;

  if (!record.SerializeToString(&value)) {
    return Error("Failed to serialize record");
  }

  leveldb::WriteOptions options;
  options.sync = true;

  leveldb::Status status = db->Put(options, encode(action.position()), value);

  if (!status.ok()) {
    return Error(status.ToString());
  }

  // Updated the first position. Notice that we use 'min' here instead
  // of checking 'isNone()' because it's likely that log entries are
  // written out of order during catch-up (e.g. if a random bulk
  // catch-up policy is used).
  first = min(first, action.position());

  LOG(INFO) << "Persisting action (" << value.size()
            << " bytes) to leveldb took " << stopwatch.elapsed();

  // Delete positions if a truncate action has been *learned*. Note
  // that we do this in a best-effort fashion (i.e., we ignore any
  // failures to the database since we can always try again).
  if (action.has_type() && action.type() == Action::TRUNCATE &&
      action.has_learned() && action.learned()) {
    CHECK(action.has_truncate());

    stopwatch.start(); // Restart the stopwatch.

    // To actually perform the truncation in leveldb we need to remove
    // all the keys that represent positions no longer in the log. We
    // do this by attempting to delete all keys that represent the
    // first position we know is still in leveldb up to (but
    // excluding) the truncate position. Note that this works because
    // the semantics of WriteBatch are such that even if the position
    // doesn't exist (which is possible because this replica has some
    // holes), we can attempt to delete the key that represents it and
    // it will just ignore that key. This is *much* cheaper than
    // actually iterating through the entire database instead (which
    // was, for posterity, the original implementation). In addition,
    // caching the "first" position we know is in the database is
    // cheaper than using an iterator to determine the first position
    // (which was, for posterity, the second implementation).

    leveldb::WriteBatch batch;

    CHECK_SOME(first);

    // Add positions up to (but excluding) the truncate position to
    // the batch starting at the first position still in leveldb. It's
    // likely that the first position is greater than the truncate
    // position (e.g., during catch-up). In that case, we do nothing
    // because there is nothing we can truncate.
    // TODO(jieyu): We might miss a truncation if we do random (i.e.,
    // out of order) bulk catch-up and the truncate operation is
    // caught up first.
    uint64_t index = 0;
    while ((first.get() + index) < action.truncate().to()) {
      batch.Delete(encode(first.get() + index));
      index++;
    }

    // If we added any positions, attempt to delete them!
    if (index > 0) {
      // We do this write asynchronously (e.g., using default options).
      leveldb::Status status = db->Write(leveldb::WriteOptions(), &batch);

      if (!status.ok()) {
        LOG(WARNING) << "Ignoring leveldb batch delete failure: "
                     << status.ToString();
      } else {
        // Save the new first position!
        CHECK_LT(first.get(), action.truncate().to());
        first = action.truncate().to();

        LOG(INFO) << "Deleting ~" << index
                  << " keys from leveldb took " << stopwatch.elapsed();
      }
    }
  }

  return Nothing();
}
void GradientChecker<Dtype>::CheckGradientSingle(Layer<Dtype>* layer,
    const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top,
    int check_bottom, int top_id, int top_data_id, bool element_wise) {
  if (element_wise) {
    CHECK_EQ(0, layer->blobs().size());
    CHECK_LE(0, top_id);
    CHECK_LE(0, top_data_id);
    const int top_count = top[top_id]->count();
    for (int blob_id = 0; blob_id < bottom.size(); ++blob_id) {
      CHECK_EQ(top_count, bottom[blob_id]->count());
    }
  }
  // First, figure out what blobs we need to check against, and zero init
  // parameter blobs.
  vector<Blob<Dtype>*> blobs_to_check;
  vector<bool> propagate_down(bottom.size(), check_bottom == -1);
  for (int i = 0; i < layer->blobs().size(); ++i) {
    Blob<Dtype>* blob = layer->blobs()[i].get();
    caffe_set(blob->count(), static_cast<Dtype>(0), blob->mutable_cpu_diff());
    blobs_to_check.push_back(blob);
  }
  if (check_bottom == -1) {
    for (int i = 0; i < bottom.size(); ++i) {
      blobs_to_check.push_back(bottom[i]);
    }
  } else if (check_bottom >= 0) {
    CHECK_LT(check_bottom, bottom.size());
    blobs_to_check.push_back(bottom[check_bottom]);
    propagate_down[check_bottom] = true;
  }
  CHECK_GT(blobs_to_check.size(), 0) << "No blobs to check.";
  // Compute the gradient analytically using Backward
  Caffe::set_random_seed(seed_);
  // Ignore the loss from the layer (it's just the weighted sum of the losses
  // from the top blobs, whose gradients we may want to test individually).
  layer->Forward(bottom, top);
  // Get additional loss from the objective
  GetObjAndGradient(*layer, top, top_id, top_data_id);
  layer->Backward(top, propagate_down, bottom);
  // Store computed gradients for all checked blobs
  vector<shared_ptr<Blob<Dtype> > >
      computed_gradient_blobs(blobs_to_check.size());
  for (int blob_id = 0; blob_id < blobs_to_check.size(); ++blob_id) {
    Blob<Dtype>* current_blob = blobs_to_check[blob_id];
    computed_gradient_blobs[blob_id].reset(new Blob<Dtype>());
    computed_gradient_blobs[blob_id]->ReshapeLike(*current_blob);
    const int count = blobs_to_check[blob_id]->count();
    const Dtype* diff = blobs_to_check[blob_id]->cpu_diff();
    Dtype* computed_gradients =
        computed_gradient_blobs[blob_id]->mutable_cpu_data();
    caffe_copy(count, diff, computed_gradients);
  }
  // Compute derivative of top w.r.t. each bottom and parameter input using
  // finite differencing.
  // LOG(ERROR) << "Checking " << blobs_to_check.size() << " blobs.";
  for (int blob_id = 0; blob_id < blobs_to_check.size(); ++blob_id) {
    Blob<Dtype>* current_blob = blobs_to_check[blob_id];
    const Dtype* computed_gradients =
        computed_gradient_blobs[blob_id]->cpu_data();
    // LOG(ERROR) << "Blob " << blob_id << ": checking "
    //     << current_blob->count() << " parameters.";
    for (int feat_id = 0; feat_id < current_blob->count(); ++feat_id) {
      // For an element-wise layer, we only need to do finite differencing to
      // compute the derivative of top[top_id][top_data_id] w.r.t.
      // bottom[blob_id][i] only for i == top_data_id.  For any other
      // i != top_data_id, we know the derivative is 0 by definition, and simply
      // check that that's true.
      Dtype estimated_gradient = 0;
      Dtype positive_objective = 0;
      Dtype negative_objective = 0;
      if (!element_wise || (feat_id == top_data_id)) {
        // Do finite differencing.
        // Compute loss with stepsize_ added to input.
        current_blob->mutable_cpu_data()[feat_id] += stepsize_;
        Caffe::set_random_seed(seed_);
        layer->Forward(bottom, top);
        positive_objective =
            GetObjAndGradient(*layer, top, top_id, top_data_id);
        // Compute loss with stepsize_ subtracted from input.
        current_blob->mutable_cpu_data()[feat_id] -= stepsize_ * 2;
        Caffe::set_random_seed(seed_);
        layer->Forward(bottom, top);
        negative_objective =
            GetObjAndGradient(*layer, top, top_id, top_data_id);
        // Recover original input value.
        current_blob->mutable_cpu_data()[feat_id] += stepsize_;
        estimated_gradient = (positive_objective - negative_objective) /
            stepsize_ / 2.;
      }
      Dtype computed_gradient = computed_gradients[feat_id];
      Dtype feature = current_blob->cpu_data()[feat_id];
      // LOG(ERROR) << "debug: " << current_blob->cpu_data()[feat_id] << " "
      //     << current_blob->cpu_diff()[feat_id];
      if (kink_ - kink_range_ > fabs(feature)
          || fabs(feature) > kink_ + kink_range_) {
        // We check relative accuracy, but for too small values, we threshold
        // the scale factor by 1.
        Dtype scale = std::max<Dtype>(
            std::max<Dtype>(fabs(computed_gradient), fabs(estimated_gradient)), 1.);
        EXPECT_NEAR(computed_gradient, estimated_gradient, threshold_ * scale)
          << "debug: (top_id, top_data_id, blob_id, feat_id)="
          << top_id << "," << top_data_id << "," << blob_id << "," << feat_id
          << "; feat = " << feature
          << "; objective+ = " << positive_objective
          << "; objective- = " << negative_objective;
      }
      // LOG(ERROR) << "Feature: " << current_blob->cpu_data()[feat_id];
      // LOG(ERROR) << "computed gradient: " << computed_gradient
      //    << " estimated_gradient: " << estimated_gradient;
    }
  }
}
InternalTargetValue ResultSet::getColumnInternal(const int8_t* buff,
                                                 const size_t entry_idx,
                                                 const size_t target_logical_idx,
                                                 const StorageLookupResult& storage_lookup_result) const {
  CHECK(buff);
  const int8_t* rowwise_target_ptr{nullptr};
  const int8_t* keys_ptr{nullptr};
  const int8_t* crt_col_ptr{nullptr};
  const size_t key_width{query_mem_desc_.getEffectiveKeyWidth()};
  if (query_mem_desc_.output_columnar) {
    crt_col_ptr = get_cols_ptr(buff, query_mem_desc_);
  } else {
    keys_ptr = row_ptr_rowwise(buff, query_mem_desc_, entry_idx);
    const auto key_bytes_with_padding = align_to_int64(get_key_bytes_rowwise(query_mem_desc_));
    rowwise_target_ptr = keys_ptr + key_bytes_with_padding;
  }
  CHECK_LT(target_logical_idx, storage_->targets_.size());
  size_t agg_col_idx = 0;
  // TODO(alex): remove this loop, offsets can be computed only once
  for (size_t target_idx = 0; target_idx < storage_->targets_.size(); ++target_idx) {
    const auto& agg_info = storage_->targets_[target_idx];
    if (query_mem_desc_.output_columnar) {
      const auto next_col_ptr = advance_to_next_columnar_target_buff(crt_col_ptr, query_mem_desc_, agg_col_idx);
      const auto col2_ptr = (agg_info.is_agg && agg_info.agg_kind == kAVG) ? next_col_ptr : nullptr;
      const auto compact_sz2 =
          (agg_info.is_agg && agg_info.agg_kind == kAVG) ? query_mem_desc_.agg_col_widths[agg_col_idx + 1].compact : 0;
      if (target_idx == target_logical_idx) {
        const auto compact_sz1 = query_mem_desc_.agg_col_widths[agg_col_idx].compact;
        const auto i1 =
            lazyReadInt(read_int_from_buff(columnar_elem_ptr(entry_idx, crt_col_ptr, compact_sz1), compact_sz1),
                        target_logical_idx,
                        storage_lookup_result);
        if (col2_ptr) {
          const auto i2 = read_int_from_buff(columnar_elem_ptr(entry_idx, col2_ptr, compact_sz2), compact_sz2);
          return InternalTargetValue(i1, i2);
        } else {
          return InternalTargetValue(
              agg_info.sql_type.is_fp() ? i1 : int_resize_cast(i1, agg_info.sql_type.get_logical_size()));
        }
      }
      crt_col_ptr = next_col_ptr;
      if (agg_info.is_agg && agg_info.agg_kind == kAVG) {
        crt_col_ptr = advance_to_next_columnar_target_buff(crt_col_ptr, query_mem_desc_, agg_col_idx + 1);
      }
    } else {
      auto ptr1 = rowwise_target_ptr;
      if (!query_mem_desc_.target_groupby_indices.empty()) {
        CHECK_LT(target_logical_idx, query_mem_desc_.target_groupby_indices.size());
        if (query_mem_desc_.target_groupby_indices[target_logical_idx] >= 0) {
          ptr1 = keys_ptr + query_mem_desc_.target_groupby_indices[target_logical_idx] * key_width;
        }
      }
      const auto compact_sz1 = query_mem_desc_.agg_col_widths[agg_col_idx].compact
                                   ? query_mem_desc_.agg_col_widths[agg_col_idx].compact
                                   : key_width;
      const int8_t* ptr2{nullptr};
      int8_t compact_sz2{0};
      if ((agg_info.is_agg && agg_info.agg_kind == kAVG) || is_real_str_or_array(agg_info)) {
        ptr2 = rowwise_target_ptr + query_mem_desc_.agg_col_widths[agg_col_idx].compact;
        compact_sz2 = query_mem_desc_.agg_col_widths[agg_col_idx + 1].compact;
      }
      if (target_idx == target_logical_idx) {
        const auto i1 = lazyReadInt(read_int_from_buff(ptr1, compact_sz1), target_logical_idx, storage_lookup_result);
        if (agg_info.is_agg && agg_info.agg_kind == kAVG) {
          CHECK(ptr2);
          const auto i2 = read_int_from_buff(ptr2, compact_sz2);
          return InternalTargetValue(i1, i2);
        } else {
          if (agg_info.sql_type.is_string() && agg_info.sql_type.get_compression() == kENCODING_NONE) {
            CHECK(!agg_info.is_agg);
            if (!lazy_fetch_info_.empty()) {
              CHECK_LT(target_logical_idx, lazy_fetch_info_.size());
              const auto& col_lazy_fetch = lazy_fetch_info_[target_logical_idx];
              if (col_lazy_fetch.is_lazily_fetched) {
                return InternalTargetValue(reinterpret_cast<const std::string*>(i1));
              }
            }
            if (none_encoded_strings_valid_) {
              if (i1 < 0) {
                CHECK_EQ(-1, i1);
                return InternalTargetValue(static_cast<const std::string*>(nullptr));
              }
              CHECK_LT(i1, none_encoded_strings_.size());
              CHECK_LT(storage_lookup_result.storage_idx, none_encoded_strings_.size());
              const auto& none_encoded_strings_for_fragment = none_encoded_strings_[storage_lookup_result.storage_idx];
              return InternalTargetValue(&none_encoded_strings_for_fragment[i1]);
            }
            CHECK(ptr2);
            const auto str_len = read_int_from_buff(ptr2, compact_sz2);
            CHECK_GE(str_len, 0);
            return getVarlenOrderEntry(i1, str_len);
          }
          return InternalTargetValue(
              agg_info.sql_type.is_fp() ? i1 : int_resize_cast(i1, agg_info.sql_type.get_logical_size()));
        }
      }
      rowwise_target_ptr =
          advance_target_ptr(rowwise_target_ptr, agg_info, agg_col_idx, query_mem_desc_, none_encoded_strings_valid_);
    }
    agg_col_idx = advance_slot(agg_col_idx, agg_info, none_encoded_strings_valid_);
  }
  CHECK(false);
  return InternalTargetValue(int64_t(0));
}