void ModuleMerger::pushScalarProperties(Item::PropertyMap *dst, Item *srcItem) { Item *origSrcItem = srcItem; do { if (!m_seenInstancesTopDown.contains(srcItem)) { m_seenInstancesTopDown.insert(srcItem); for (auto it = srcItem->properties().constBegin(); it != srcItem->properties().constEnd(); ++it) { const ValuePtr &srcVal = it.value(); if (srcVal->type() != Value::JSSourceValueType) continue; const PropertyDeclaration srcDecl = srcItem->propertyDeclaration(it.key()); if (!srcDecl.isValid() || !srcDecl.isScalar()) continue; ValuePtr &v = (*dst)[it.key()]; if (v) continue; ValuePtr clonedVal = srcVal->clone(); m_decls[clonedVal] = srcDecl; clonedVal->setDefiningItem(origSrcItem); v = clonedVal; } } srcItem = srcItem->prototype(); } while (srcItem && srcItem->isModuleInstance()); }
void ModuleMerger::insertProperties(Item::PropertyMap *dst, Item *srcItem, PropertiesType type) { QSet<const Item *> &seenInstances = type == ScalarProperties ? m_seenInstancesTopDown : m_seenInstancesBottomUp; Item *origSrcItem = srcItem; do { if (!seenInstances.contains(srcItem)) { seenInstances.insert(srcItem); for (Item::PropertyMap::const_iterator it = srcItem->properties().constBegin(); it != srcItem->properties().constEnd(); ++it) { const ValuePtr &srcVal = it.value(); if (srcVal->type() != Value::JSSourceValueType) continue; const PropertyDeclaration srcDecl = srcItem->propertyDeclaration(it.key()); if (!srcDecl.isValid() || srcDecl.isScalar() != (type == ScalarProperties)) continue; ValuePtr &v = (*dst)[it.key()]; if (v && type == ScalarProperties) continue; ValuePtr clonedVal = srcVal->clone(); m_decls[clonedVal] = srcDecl; clonedVal->setDefiningItem(origSrcItem); if (v) { QBS_CHECK(!clonedVal->next()); clonedVal->setNext(v); } v = clonedVal; } } srcItem = srcItem->prototype(); } while (srcItem && srcItem->type() == ItemType::ModuleInstance); }
ValuePtr sCdr(EnvPtr env, ValuePtr args) { CHECK("Number of arguments", sLength(args) == 1); CHECK("Argument is pair", args->car()->isPair()); return args->car()->cdr(); }
ValuePtr evalIf(EnvPtr env, ValuePtr statement) { int length = sLength(statement); if(length == 3) { if(sEqP(eval(env, statement->cdr()->car()), rsFalse()) == false) { return eval(env, statement->cdr()->cdr()->car()); } else { return rsUndefined(); } } else if(length == 4) { if(sEqP(eval(env, statement->cdr()->car()), rsFalse()) == false) { return eval(env, statement->cdr()->cdr()->car()); } else { return eval(env, statement->cdr()->cdr()->cdr()->car()); } } else { CHECK_FAIL("Wrong number of arguments to if"); return rsUndefined(); } }
void TestEval::eval_apply() { EnvPtr env = libraryEnvironment(); ValuePtr result = eval(env, "(list? '(foo bar baz))"); CHECK("Result is boolean", result->isBool() && result->vBool()); }
void DisassemblerContext::print_exists(const ValuePtr<Exists>& term, bool bracket) { if (bracket) *m_output << '('; *m_output << "exists ("; unsigned n_parameters = term->parameter_types().size(); unsigned parameter_name_base = m_parameter_name_index; m_parameter_name_index += n_parameters; boost::format name_formatter("%%%s"); m_parameter_names.push_back(ParameterNameList::value_type()); ParameterNameList::value_type& name_list = m_parameter_names.back(); for (unsigned ii = 0; ii != n_parameters; ++ii) { if (ii) *m_output << ", "; std::string name = str(name_formatter % (parameter_name_base + ii)); *m_output << name << " : "; print_term(term->parameter_types()[ii], false); name_list.push_back(name); } *m_output << ") > "; print_term(term->result(), true); m_parameter_names.pop_back(); m_parameter_name_index = parameter_name_base; if (bracket) *m_output << ')'; }
ValuePtr ModuleMerger::lastInNextChain(const ValuePtr &v) { ValuePtr n = v; while (n->next()) n = n->next(); return n; }
void ObjectImpl::encodeProperties(qpid::framing::Buffer& buffer) const { int propCount = objectClass->getPropertyCount(); uint8_t bit = 0; uint8_t mask = 0; ValuePtr value; for (int idx = 0; idx < propCount; idx++) { const SchemaProperty* prop = objectClass->getProperty(idx); if (prop->isOptional()) { value = properties[prop->getName()]; if (bit == 0) bit = 1; if (!value->isNull()) mask |= bit; if (bit == 0x80) { buffer.putOctet(mask); bit = 0; mask = 0; } else bit = bit << 1; } } if (bit != 0) { buffer.putOctet(mask); } for (int idx = 0; idx < propCount; idx++) { const SchemaProperty* prop = objectClass->getProperty(idx); value = properties[prop->getName()]; if (!prop->isOptional() || !value->isNull()) { value->impl->encode(buffer); } } }
ValuePtr apply(EnvPtr env, ValuePtr procedure, ValuePtr args) { if(procedure->type() == Value::NATIVE_PROCEDURE) { NativeProcedureValue* proc = static_cast<NativeProcedureValue*>(procedure.mValue); return (*proc->mProc)(env, args); } else if(procedure->type() == Value::PROCEDURE) { EnvPtr callEnvironment = new Environment; ProcedureValue* proc = static_cast<ProcedureValue*>(procedure.mValue); callEnvironment->parent = proc->environment; int iParam = 0; while(args->isNull() == false) { if(iParam == static_cast<int>(proc->paramList.size())) { CHECK_FAIL("Too many arguments to procedure"); } callEnvironment->values[proc->paramList[iParam]] = args->car(); iParam++; args = args->cdr(); } if(iParam != static_cast<int>(proc->paramList.size())) { CHECK_FAIL("Too few arguments to procedure"); } return evalSequence(callEnvironment, proc->body); } else { sWrite(env, new PairValue(procedure, new PairValue())); CHECK_FAIL("Wrong type of argument to apply: not procedure"); return NULL; } }
void PackedValue::Unpack() const { if (m_packedDataLayout && (m_packedDataLayout->GetNumTimeSteps() != 1) && (m_packedDataLayout->GetNumSequences() != 1) && Internal::IsAutomaticUnpackingOfPackedValuesDisabled()) LogicError("PackedValue::Unpack: Automatic unpacking of PackedValue objects is disabled"); if (m_isPacked) { ValuePtr valueObject; auto dataType = m_packedData->GetDataType(); switch (dataType) { case DataType::Float: valueObject = CompositeFunction::GetValueObjectFromCNTKImplMatrixAndMBLayout(m_sampleShape, *(m_packedData->GetMatrix<float>()), m_packedDataLayout, m_isReadOnly); break; case DataType::Double: valueObject = CompositeFunction::GetValueObjectFromCNTKImplMatrixAndMBLayout(m_sampleShape, *(m_packedData->GetMatrix<double>()), m_packedDataLayout, m_isReadOnly); break; default: LogicError("Unsupported DataType %s", DataTypeName(dataType)); } m_data = valueObject->Data(); m_mask = valueObject->Mask(); m_packedData = nullptr; m_packedDataLayout = nullptr; m_isPacked = false; if (m_unpackedShape != m_data->Shape()) LogicError("The computed unpacked shape of the PackedValue object does not match the actual Data NDArrayView's shape after unpacking"); } }
bool sListP(ValuePtr data) { while(data->isPair() && !data->isNull()) data = data->cdr(); return data->isNull(); }
void RunEvaluationOneHidden(FunctionPtr evalFunc, const DeviceDescriptor& device) { const std::wstring inputNodeName = L"features"; const std::wstring outputNodeName = L"out.z_output"; Variable inputVar; if (!GetInputVariableByName(evalFunc, inputNodeName, inputVar)) { fprintf(stderr, "Input variable %S is not available.\n", inputNodeName.c_str()); throw("Input variable not found error."); } Variable outputVar; if (!GetOutputVaraiableByName(evalFunc, outputNodeName, outputVar)) { fprintf(stderr, "Output variable %S is not available.\n", outputNodeName.c_str()); throw("Output variable not found error."); } // Evaluate the network in several runs size_t iterationCount = 4; size_t numSamples = 3; for (size_t t = 0; t < iterationCount; ++t) { std::vector<float> inputData(inputVar.Shape().TotalSize() * numSamples); for (size_t i = 0; i < inputData.size(); ++i) { inputData[i] = static_cast<float>(i % 255); } NDShape inputShape = inputVar.Shape().AppendShape({1, numSamples}); ValuePtr inputValue = MakeSharedObject<Value>(MakeSharedObject<NDArrayView>(inputShape, inputData, true)); ValuePtr outputValue; std::unordered_map<Variable, ValuePtr> outputs = {{outputVar, outputValue}}; evalFunc->Forward({{inputVar, inputValue}}, outputs, device); outputValue = outputs[outputVar]; NDShape outputShape = outputVar.Shape().AppendShape({1, numSamples}); std::vector<float> outputData(outputShape.TotalSize()); NDArrayViewPtr cpuArrayOutput = MakeSharedObject<NDArrayView>(outputShape, outputData, false); cpuArrayOutput->CopyFrom(*outputValue->Data()); assert(outputData.size() == outputVar.Shape()[0] * numSamples); fprintf(stderr, "Evaluation result:\n"); size_t dataIndex = 0; auto outputDim = outputVar.Shape()[0]; for (size_t i = 0; i < numSamples; i++) { fprintf(stderr, "Iteration:%lu, Sample %lu:\n", t, i); fprintf(stderr, "Ouput:"); for (size_t j = 0; j < outputDim; j++) { fprintf(stderr, "%f ", outputData[dataIndex++]); } fprintf(stderr, "\n"); } } }
void CheckValue(const ValuePtr testValue, const size_t dimension, const vector<vector<size_t>>& expectedData, const vector<size_t>& seqLenList, const vector<bool>& seqStartFlags = {}) { // Check parameters BOOST_TEST(expectedData.size() == seqLenList.size(), "Parameter error: the sequence number in the exepected data and sequence list does not match."); for (size_t i = 0; i < expectedData.size(); i++) { if (expectedData[i].size() != seqLenList[i]) { ReportFailure("Parameter erroe: the number of data for sequence %" PRIu64 " in the expected data does not match. Expected: %" PRIu64 ", actual: %" PRIu64 ".", i, seqLenList[i], expectedData[i].size()); } } // Check shape NDShape shape = testValue->Shape(); size_t valueRank = shape.Rank(); if (valueRank < 2 || valueRank > 3 || shape[0] != dimension) { ReportFailure("The shape of the value does not match\n"); } size_t numOfSequences = valueRank == 2 ? 1 : shape[2]; if (numOfSequences != expectedData.size()) { ReportFailure("The sequence number in the Value does not match. Expected: %" PRIu64 ", actual: %" PRIu64 ".", expectedData.size(), numOfSequences); } CheckMask(testValue, seqLenList, seqStartFlags); // Get data from Value vector<ElementType> outputData(shape.TotalSize()); NDArrayViewPtr arrayOutput = MakeSharedObject<NDArrayView>(shape, outputData, false); arrayOutput->CopyFrom(*testValue->Data()); size_t maxSeqLen = *max_element(seqLenList.begin(), seqLenList.end()); size_t oIndex = 0; for (size_t seq = 0; seq < seqLenList.size(); seq++) { size_t seqLen = seqLenList[seq]; for (size_t sample = 0; sample < seqLen; sample++) { for (size_t c = 0; c < dimension; c++, oIndex++) { if (outputData[oIndex] != 0) { if (outputData[oIndex] != 1) { ReportFailure("OneHot vector contains value other than 0 and 1 at seqNo=%" PRIu64 " sampleNo=%" PRIu64 " position=%" PRIu64 "\n", seq, sample, c); } if (c != expectedData[seq][sample]) { ReportFailure("OneHot Index does match at seqNo=%" PRIu64 ", sampleNo=%" PRIu64 ", expected: %" PRIu64 ", actual: %" PRIu64 "\n", seq, sample, expectedData[seq][sample], c); } } } } // Skip mask data oIndex += (maxSeqLen - seqLen) * dimension; } }
void TestEval::eval_define() { EnvPtr env = libraryEnvironment(); ValuePtr result = eval(env, "(define a 10) (define (foo x) (set! a x)) (let ((b 5)) (foo 20)) a"); CHECK("Result is integer", result->isNumber() && result->isExact()); CHECK_EQUAL("Value is correct", int, 20, result->vInt()); }
ValuePtr Dict::copy(bool deep) const { ValuePtr c = new Dict; for (unsigned i = 0; i < size(); i++) c->insert(keyAt(i), deep ? get(i)->copy(true) : get(i)); return c; }
bool sPureInteger(ValuePtr args) { while(args->isNull() == false) { if(!(args->car()->isNumber() && args->car()->isExact())) return false; args = args->cdr(); } return true; }
void DisassemblerContext::setup_block(const ValuePtr<Block>& block) { if (block->dominator()) setup_term_name(block->dominator()); for (Block::PhiList::const_iterator ii = block->phi_nodes().begin(), ie = block->phi_nodes().end(); ii != ie; ++ii) setup_term_definition(*ii); for (Block::InstructionList::const_iterator ii = block->instructions().begin(), ie = block->instructions().end(); ii != ie; ++ii) setup_term_definition(*ii); }
ValuePtr apply(EnvPtr env, ValuePtr statement) { if(!sListP(statement)) { CHECK_FAIL("Error in apply: not list"); } ValuePtr procedure = statement->car(); ValuePtr args = statement->cdr(); return apply(env, procedure, args); }
ValuePtr sStringAppend(EnvPtr env, ValuePtr args) { ostringstream ossStream; while(args->isNull() == false) { if(args->car()->isString()) ossStream << args->car()->vString(); args = args->cdr(); } return new StringValue(ossStream.str()); }
void TestLibrary::strings() { EnvPtr env = libraryEnvironment(); ValuePtr result; // number->string result = eval(env, "(string-append \"abc\" \"def\")"); CHECK("Result is string", result->isString()); CHECK_EQUAL("Correct value", string, "abcdef", result->vString()); }
float sAsFloat(ValuePtr number) { CHECK("Error convering non number to float", number->isNumber()); if(!number->isExact()) { return number->vFloat(); } else { return static_cast<float>(number->vInt()); } }
int main(int argc, char *argv[]) { try { Reader reader(cin); ValuePtr data = reader.parse(); if (!data.isNull()) cout << *data; return 0; } CBANG_CATCH_ERROR; return 0; }
void DisassemblerContext::print_block(const ValuePtr<Block>& block, const TermDefinitionList& definitions) { *m_output << "block " << name(block); if (block->dominator()) *m_output << '(' << name(block->dominator()) << ')'; *m_output << ":\n"; for (Block::PhiList::const_iterator ii = block->phi_nodes().begin(), ie = block->phi_nodes().end(); ii != ie; ++ii) { *m_output << " "; print_term_definition(*ii); } print_definitions(definitions, " "); }
size_t Evaluator::GetSampleCount(const Variable& var, const ValuePtr& value) { auto valueDataShape = value->Shape(); size_t numMaskedSamples = value->MaskedCount(); size_t numSamplesInDataArrayView = valueDataShape.SubShape(var.Shape().Rank()).TotalSize(); if (numMaskedSamples > numSamplesInDataArrayView) LogicError("Number (%d) of masked values cannot exceed the number (%d) of samples that the Value object's Data NDArrayView can hold.", (int)numMaskedSamples, (int)numSamplesInDataArrayView); return (numSamplesInDataArrayView - numMaskedSamples); }
ValuePtr sEnableModule(EnvPtr env, ValuePtr args) { using SSM::EVA; using SSM::Controller; if(sLength(args) == 1 && args->car()->isString()) { EVA* evaScreen = Controller::getSingleton()->evaScreen; evaScreen->enableModule(string(args->car()->vString())); } return rsUndefined(); }
ff::SharedResourceValue ff::Dict::GetResource(ff::StringRef name) const { Value *value = GetValue(name); ValuePtr newValue; if (value && value->Convert(Value::Type::Resource, &newValue)) { return newValue->AsResource(); } return ff::SharedResourceValue(); }
void DisassemblerContext::print_function(const ValuePtr<Function>& term) { print_function_type_term(term->function_type(), term); if (!term->blocks().empty()) { *m_output << " {\n"; for (Function::BlockList::const_iterator ii = term->blocks().begin(), ie = term->blocks().end(); ii != ie; ++ii) print_block(*ii, m_local_definitions[*ii]); *m_output << "}"; } *m_output << ";\n"; }
int sLength(ValuePtr data) { int length = 0; ValuePtr ptr = data; while(ptr->isNull() == false) { if(ptr->isPair() == false) CHECK_FAIL("Malformed list to length"); length++; ptr = ptr->cdr(); } return length; }
ValuePtr sGetPositiveCapacity(EnvPtr env, ValuePtr args) { using namespace SSM; if(sLength(args) == 1 && args->car()->isSymbol()) { if(args->car()->vString() == "crew") { return new NumberValue(Craft::getSingleton()->getLocalResourcePositiveCapacity(CREW_SPACE)); } } return rsUndefined(); }
ValuePtr evalOr(EnvPtr env, ValuePtr statement) { ValuePtr args = statement->cdr(); while(args->isNull() == false) { ValuePtr test = eval(env, args->car()); if(!sEqP(test, rsFalse())) return rsTrue(); args = args->cdr(); } return rsFalse(); }